How to Connect Odoo to PostgreSQL Using TCP/IP and Unix Domain Sockets

Odoo relies on postgres for storing and retrieving all application data, making the database connection an important part of overall system performance. When both Odoo and PostgreSQL run on the same server, the application can connect to the database in two different ways: through a TCP/IP connection or a Unix domain socket.

Although both methods provide access to the same database, they differ in how the operating system establishes the connection and handles communication. In this blog, we'll configure Odoo to use both connection methods, examine the postgres logs generated by each, compare their connection details, and measure the time required to establish database sessions. This comparison helps identify the most suitable connection method for local Odoo deployments.

Check the current postgres clusters.

cybrosys@cybrosys:~$ pg_lsclusters 

Result:

Ver Cluster Port Status Owner    Data directory              Log file
17  main    5434 online postgres /var/lib/postgresql/17/main /var/log/postgresql/postgresql-17-main.log
18  main    5432 online postgres /var/lib/postgresql/18/main /var/log/postgresql/postgresql-18-main.log
19  main    5433 online postgres /var/lib/postgresql/19/main /var/log/postgresql/postgresql-19-main.log

We can connect odoo to psql in two ways:

  • Unix socket
  • Over tcp

Before connecting odoo to postgres, set the appropriate values for the logging-related parameters in PostgreSQL like this.

Check the path of the postgres configuration file like this.

show config_file ;

Result:

               config_file               
-----------------------------------------
 /etc/postgresql/18/main/postgresql.conf
(1 row)

Open the postgres configuration file like this.

sudo nano /etc/postgresql/18/main/postgresql.conf

Set the values for the log-related parameters like this.

 log_connections = 'all';
log_disconnections = on;
log_line_prefix ='%m [%p] user=%u db=%d app=%a remote=%r local=%L ';

Now, restart the postgresql.

sudo systemctl restart postgresql

Now, open the psql terminal like this.

cybrosys@cybrosys:~$ sudo su postgres
postgres@cybrosys:/home/cybrosys$ psql

Result:

LOG:  connection ready: setup total=8.513 ms, fork=1.154 ms, authentication=0.283 ms
psql (19rc1, server 18.6 (Ubuntu 18.6-1.pgdg22.04+2))
Type "help" for help.
postgres=#

Now, you can see that the logging is enabled.

Now, connect to postgres through TCP like this:

cybrosys@cybrosys:~$ sudo su postgres
[sudo] password for cybrosys: 
postgres@cybrosys:/home/cybrosys$ psql -h localhost

Result:

Password for user postgres: 
psql (19rc1, server 18.6 (Ubuntu 18.6-1.pgdg22.04+2))
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off, ALPN: postgresql)
Type "help" for help.
postgres=# 

Checking the log file during the connection to postgres over a unix socket like this.

cybrosys@cybrosys:~$ sudo tail -f /var/log/postgresql/postgresql-18-main.log

Result:

2026-10-31 15:41:46.244 IST [344716] LOG:  database system was shut down at 2026-10-31 15:41:46 IST
2026-10-31 15:41:46.256 IST [344704] LOG:  database system is ready to accept connections
2026-10-31 15:43:36.133 IST [344704] LOG:  received SIGHUP, reloading configuration files
2026-10-31 15:43:36.134 IST [344704] LOG:  parameter "log_connections" changed to "all"
2026-10-31 15:43:36.134 IST [344704] LOG:  parameter "log_disconnections" changed to "on"
2026-10-31 15:43:36.134 IST [344704] user= db= app= remote= local=[none] LOG:  parameter "log_line_prefix" changed to "%m [%p] user=%u db=%d app=%a remote=%r local=%L "
2026-10-31 15:43:50.304 IST [347910] user=[unknown] db=[unknown] app=[unknown] remote=[local] local=[local] LOG:  connection received: host=[local]
2026-10-31 15:43:50.305 IST [347910] user=postgres db=postgres app=[unknown] remote=[local] local=[local] LOG:  connection authenticated: identity="postgres" method=peer (/etc/postgresql/18/main/pg_hba.conf:118)
2026-10-31 15:43:50.305 IST [347910] user=postgres db=postgres app=[unknown] remote=[local] local=[local] LOG:  connection authorized: user=postgres database=postgres application_name=psql
2026-10-31 15:43:50.306 IST [347910] user=postgres db=postgres app=psql remote=[local] local=[local] LOG:  connection ready: setup total=3.559 ms, fork=1.162 ms, authentication=0.243 ms
2026-10-31 15:44:31.146 IST [347910] user=postgres db=postgres app=psql remote=[local] local=[local] LOG:  disconnection: session time: 0:00:40.842 user=postgres database=postgres host=[local]
2026-10-31 15:44:39.958 IST [349174] user=[unknown] db=[unknown] app=[unknown] remote=[local] local=[local] LOG:  connection received: host=[local]
2026-10-31 15:44:39.959 IST [349174] user=postgres db=postgres app=[unknown] remote=[local] local=[local] LOG:  connection authenticated: identity="postgres" method=peer (/etc/postgresql/18/main/pg_hba.conf:118)

The PostgreSQL log shows that the connection was made properly through TCP/IP. First, PostgreSQL notes that it got a client connection. Then, it checks the user's identity using the way set up in pg_hba.conf allows the session for the database that was asked for. Then says the connection is all set after finishing the setup. The log also keeps track of how long it took to make the connection, including creating the process and checking the identity. When the client ends the connection, PostgreSQL writes down how long the session lasted and the name of the database user, giving a record of the whole connection process.

Now, connect to psql through a Unix socket and execute the queries below.

cybrosys@cybrosys:~$ sudo su postgres
postgres@cybrosys:/home/cybrosys$ psql -h /var/run/postgresql/

Result:

psql (19rc1, server 18.6 (Ubuntu 18.6-1.pgdg22.04+2))
Type "help" for help.
postgres=# 

Now, check if SSL is enabled when we log in to psql using a Unix socket.

SELECT ssl
FROM pg_stat_ssl
WHERE pid = pg_backend_pid();

Result:

 ssl 
-----
 f
(1 row)

Check the client address like this.

SELECT client_addr
FROM pg_stat_activity
WHERE pid = pg_backend_pid();

Result:

 client_addr 
-------------
 
(1 row)

Check the client port also.

 SELECT client_port
FROM pg_stat_activity
WHERE pid = pg_backend_pid();

Result:

 client_port 
-------------
          -1
(1 row)

Check the Client IP address if the connection is over TCP/IP.

 SELECT inet_client_addr();

Result:

 inet_client_addr 
------------------
 
(1 row)

Check the address of the PostgreSQL server for the current connection.

 SELECT inet_server_addr();

Result:

 inet_server_addr 
------------------
 
(1 row)

Check the port number of the postgres server that accepted the current connection.

 SELECT inet_server_port();

Result:

 inet_server_port 
------------------
                 
(1 row)

Check the metadata from the catalogue named pg_stat_ssl based on the current process ID.

 SELECT *
FROM pg_stat_ssl
WHERE pid = pg_backend_pid();

Result:

  pid   | ssl | version | cipher | bits | client_dn | client_serial | issuer_dn 
--------+-----+---------+--------+------+-----------+---------------+-----------
 370291 | f   |         |        |      |           |               | 
(1 row)

Check the metadata related to the GSSAPI-authenticated client connections.

 SELECT *
FROM pg_stat_gssapi
WHERE pid = pg_backend_pid();

Result :

  pid   | gss_authenticated | principal | encrypted | credentials_delegated 
--------+-------------------+-----------+-----------+-----------------------
 370291 | f                 |           | f         | f
(1 row)

Check the metadata related to the connection info.

 \conninfo

Result:

           Connection Information
      Parameter       |        Value        
----------------------+---------------------
 Database             | postgres
 Client User          | postgres
 Socket Directory     | /var/run/postgresql
 Server Port          | 5432
 Options              | 
 Protocol Version     | 3.0
 Password Used        | false
 GSSAPI Authenticated | false
 Backend PID          | 370291
 SSL Connection       | false
 Superuser            | on
 Hot Standby          | off
(12 rows)

Now, connect to postgres using the TCP method like this and check the same queries again.

postgres@cybrosys:/home/cybrosys$ psql -h localhost

Result:

Password for user postgres: 
LOG:  connection ready: setup total=13.332 ms, fork=1.249 ms, authentication=5.907 ms
psql (19rc1, server 18.6 (Ubuntu 18.6-1.pgdg22.04+2))
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off, ALPN: postgresql)
Type "help" for help.

Checking the SSL-related metadata from the catalogue named pg_stat_ssl.

 SELECT ssl
FROM pg_stat_ssl
WHERE pid = pg_backend_pid();

Result:

 ssl 
-----
 t
(1 row)

Check the client address of the current process ID.

 SELECT client_addr
FROM pg_stat_activity
WHERE pid = pg_backend_pid();

Result:

 client_addr 
-------------
 127.0.0.1
(1 row)

Check the client port also.

 SELECT client_port
FROM pg_stat_activity
WHERE pid = pg_backend_pid();

Result:

 client_port 
-------------
       55930
(1 row)

Check the client address.

 SELECT inet_client_addr();

Result:

 inet_client_addr 
------------------
 127.0.0.1
(1 row)

Check the server address of postgres where the current connection is accepted.

 SELECT inet_server_addr();

Result:

 inet_server_addr 
------------------
 127.0.0.1
(1 row)

Check the server port also.

  SELECT inet_server_port();

Result:

 inet_server_port 
------------------
             5432
(1 row)

Check the SSL-related metadata like this.

 SELECT *
FROM pg_stat_ssl
WHERE pid = pg_backend_pid();

Result:

  pid   | ssl | version |         cipher         | bits | client_dn | client_serial | issuer_dn 
--------+-----+---------+------------------------+------+-----------+---------------+-----------
 372428 | t   | TLSv1.3 | TLS_AES_256_GCM_SHA384 |  256 |           |               | 
(1 row)

Check the metadata related to the GSSAPI-authenticated client connections.

  SELECT *
FROM pg_stat_gssapi
WHERE pid = pg_backend_pid();

Result:

  pid   | gss_authenticated | principal | encrypted | credentials_delegated 
--------+-------------------+-----------+-----------+-----------------------
 372428 | f                 |           | f         | f
(1 row)

Check the result of the connection info also.

 \conninfo 

Result:

            Connection Information
      Parameter       |         Value          
----------------------+------------------------
 Database             | postgres
 Client User          | postgres
 Host                 | localhost
 Host Address         | 127.0.0.1
 Server Port          | 5432
 Options              | 
 Protocol Version     | 3.0
 Password Used        | true
 GSSAPI Authenticated | false
 Backend PID          | 372428
 SSL Connection       | true
 SSL Library          | OpenSSL
 SSL Protocol         | TLSv1.3
 SSL Key Bits         | 256
 SSL Cipher           | TLS_AES_256_GCM_SHA384
 SSL Compression      | false
 ALPN                 | postgresql
 Superuser            | on
 Hot Standby          | off
(19 rows)

Key differences between connecting to Postgres via TCP and Unix socket

Feature / Query Unix Socket Connection (psql -h /var/run/postgresql/) TCP Connection (psql -h localhost)
Connection Type Unix Domain Socket TCP/IP
Authentication Peer (no password) Password authentication
SSL Enabled (pg_stat_ssl.ssl) false true
Client Address (pg_stat_activity.client_addr) NULL 127.0.0.1
Client Port (pg_stat_activity.client_port) -1 55930 (ephemeral port)
Client IP (inet_client_addr()) NULL 127.0.0.1
Server Address (inet_server_addr()) NULL 127.0.0.1
Server Port (inet_server_port()) NULL 5432
SSL Version (pg_stat_ssl.version) NULL TLSv1.3
SSL Cipher (pg_stat_ssl.cipher) NULL TLS_AES_256_GCM_SHA384
SSL Key Bits (pg_stat_ssl.bits) NULL 256
GSSAPI Authenticated (pg_stat_gssapi.gss_authenticated) false false
GSSAPI Encryption (pg_stat_gssapi.encrypted) false false
Kerberos Principal (pg_stat_gssapi.principal) NULL NULL
Socket Directory (\conninfo) /var/run/postgresql Not Applicable
Host (\conninfo) Not Applicable localhost
Host Address (\conninfo) Not Applicable 127.0.0.1
Password Used (\conninfo) false true
SSL Connection (\conninfo) false true
SSL Library (\conninfo) Not Applicable OpenSSL
Protocol Version 3.0 3.0
Server Port (\conninfo) 5432 5432
Backend PID 370291 (varies) 372428 (varies)
Superuser on on
Hot Standby off off

Odoo connects to postgres through TCP.

[options]
admin_passwd = cool
db_host = localhost
db_port = 5432
db_user = cybrosys
db_password = cool
xmlrpc_port = 8079
; db_name = demo
addons_path = /home/cybrosys/odoo_17/addons,

Now, check the odoo server log.

2026-10-31 10:32:11,188 376791 INFO ? odoo: Odoo version 17.0 
2026-10-31 10:32:11,189 376791 INFO ? odoo: Using configuration file at /home/cybrosys/odoo_17/odoo.conf 
2026-10-31 10:32:11,189 376791 INFO ? odoo: addons paths: ['/home/cybrosys/odoo_17/odoo/addons', '/home/cybrosys/.local/share/Odoo/addons/17.0', '/home/cybrosys/odoo_17/addons''] 
2026-10-31 10:32:11,189 376791 INFO ? odoo: database: cybrosys@localhost:5432 
2026-10-31 10:32:11,488 376791 INFO ? odoo.addons.base.models.ir_actions_report: You need Wkhtmltopdf to print a pdf version of the reports. 
2026-10-31 10:32:12,039 376791 WARNING ? odoo.api.create: The model odoo.addons.cyllo_base.models.recent_apps is not overriding the create method in batch 
2026-10-31 10:32:12,064 376791 INFO ? odoo.service.server: HTTP service (werkzeug) running on cybrosys:8079 
LOG:  connection ready: setup total=37.345 ms, fork=2.321 ms, authentication=16.161 ms
LOG:  connection ready: setup total=28.501 ms, fork=2.440 ms, authentication=13.407 ms

Odoo connects to postgres through a Unix socket.


[options]
admin_passwd = cool
db_host = /var/run/postgresql
db_port = 5432
db_user = cybrosys
db_password = cool
xmlrpc_port = 8079
; db_name = demo
addons_path = /home/cybrosys/odoo_17/addons

Now, check the odoo server log.

/home/cybrosys/odoo_17/.venv/bin/python /home/cybrosys/odoo_17/odoo-bin -c odoo.conf 
2026-10-31 10:34:00,576 379861 INFO ? odoo: Odoo version 17.0 
2026-10-31 10:34:00,576 379861 INFO ? odoo: Using configuration file at /home/cybrosys/odoo_17/cyllo.conf 
2026-10-31 10:34:00,576 379861 INFO ? odoo: addons paths: ['/home/cybrosys/odoo_17/odoo/addons', '/home/cybrosys/.local/share/Odoo/addons/17.0', '/home/cybrosys/odoo_17/addons''] 
2026-10-31 10:34:00,576 379861 INFO ? odoo: database: cybrosys@/var/run/postgresql:5432 
2026-10-31 10:34:00,655 379861 INFO ? odoo.addons.base.models.ir_actions_report: You need Wkhtmltopdf to print a pdf version of the reports. 
2026-10-31 10:34:00,814 379861 WARNING ? odoo.api.create: The model odoo.addons.cyllo_base.models.recent_apps is not overriding the create method in batch 
2026-10-31 10:34:00,821 379861 INFO ? odoo.service.server: HTTP service (werkzeug) running on cybrosys:8079 
LOG:  connection ready: setup total=3.961 ms, fork=1.231 ms, authentication=0.292 ms
LOG:  connection ready: setup total=4.166 ms, fork=1.657 ms, authentication=0.294 ms

Connection Time Comparison

Metric TCP (localhost) Unix Socket (/var/run/postgresql)
Setup Time (1st connection) 37.345 ms 3.961 ms
Setup Time (2nd connection) 28.501 ms 4.166 ms
Authentication Time (1st) 16.161 ms 0.292 ms
Authentication Time (2nd) 13.407 ms 0.294 ms
Approximate Improvement — 7–10× faster

If Odoo and PostgreSQL are on the server, setting db_host to the PostgreSQL Unix socket folder, such as /var/run/postgresql, makes connecting much faster than using localhost.

The shorter setup and login times can make the Odoo application feel quicker, especially when many PostgreSQL connections are created.

Both TCP/IP and Unix domain sockets let Odoo talk to PostgreSQL. TCP/IP and Unix domain sockets use different ways to connect. TCP/IP uses the operating system's network system and can use SSL and network authentication, so TCP/IP works well for database servers that are far away. Unix domain sockets instead talk to the local operating system and do not use the network layer.

This makes Unix domain sockets have lower connection overhead and faster setup. When Odoo and PostgreSQL live on the same machine, Unix domain sockets give a faster, more efficient connection. Knowing these differences lets administrators pick the connection method that best fits the deployment and speed needs.

WhatsApp