How to Install psql on Ubuntu: Client and Server

Installing psql on Ubuntu: Client and Server

Setting up PostgreSQL on Ubuntu starts with deciding whether the database will run on this machine or another one. psql is a terminal client for sending SQL commands to a PostgreSQL server.

I’ll show how to select the package source and create an application role and database.

TL;DR

Install the PostgreSQL server for a local database, or install only the client tools when connecting to another host.

  • For a local server, Ubuntu documents sudo apt install postgresql.
  • For client tools only, install the matching postgresql-client package.
  • Use sudo -u postgres psql for the local administrator connection, then create a separate login role and database for an application.

What Is psql on Ubuntu?

psql is PostgreSQL’s interactive terminal client. It sends SQL commands to a PostgreSQL server, which stores and processes the data. A client can connect to a server on another machine, so installing psql does not always mean hosting a database locally.

Ubuntu separates the server package from its client utilities. The server package runs the database service. Client utilities include psql and are enough for a remote connection.

I checked psql --version. It printed:

psql (PostgreSQL) 18.6 (Ubuntu 18.6-1.pgdg24.04+2)
Terminal output showing psql client version 18.6
The psql version command reports the installed client version.

That version output confirms the client executable is present, but it says nothing about whether a server is running or reachable. Choose the Ubuntu archive for the distribution package set.

Use PGDG when you need to select a particular supported upstream major version. Its repository instructions specify the matching package source.

How to Install PostgreSQL and psql on Ubuntu

The Ubuntu package manager installs the distribution-supported server. These install commands change the machine, so run them on the Ubuntu system you intend to configure.

Step 1: Choose the package source

Install the server package when the database should run on this machine. For a client-only machine, install the versioned client package available for that Ubuntu release, such as postgresql-client. Check the package candidate before selecting a major-version suffix.

sudo apt update
sudo apt install postgresql

If your application requires a particular supported major version, follow the current PGDG Ubuntu instructions to add its repository and select the matching package. Avoid mixing package sources casually.

Step 2: Open the local PostgreSQL prompt

Ubuntu’s default local administration path uses the operating-system account named postgres to connect as the database role named postgres. Peer authentication checks the operating-system identity on a local socket, which is why switching Linux users is not the same as setting a database password.

sudo -u postgres psql

At the postgres=# prompt, SQL statements end with a semicolon. Leave psql with \q. Give an application its own role with only the permissions it needs.

Step 3: Create an application role and database

A PostgreSQL role is a database identity, not a Linux login. Create a login role and a database it owns from the administrative prompt, replacing the example names with identifiers suitable for your application.

CREATE ROLE appuser WITH LOGIN PASSWORD 'replace-with-a-secret';
CREATE DATABASE appdb OWNER appuser;

Use a secret manager or a protected configuration file for the password rather than committing it to source control. To connect locally with password authentication, specify the role and database explicitly: psql -h 127.0.0.1 -U appuser -d appdb. TCP localhost follows host rules in pg_hba.conf, which can differ from peer authentication on a Unix socket.

Common psql Errors on Ubuntu

A failed psql connection usually points to the selected server, role, database or authentication rule, not a missing command. Check the requested connection explicitly before changing authentication settings.

SymptomLikely causeNext check
psql: command not foundClient tools are absent or not on PATHInstall the client package for the Ubuntu release
Connection refusedNo server is listening at the selected host and portConfirm the server package and service on the database host
Peer authentication failedLocal OS username does not match the requested database roleUse the postgres OS account for administration or configure a suitable login path
Role or database does not existpsql defaulted to the current name, or the target was mistypedPass -U role and -d database explicitly

Avoid changing every local rule to password authentication as a first fix. Ubuntu stores configuration under /etc/postgresql/<version>/main. Inspect the pg_hba.conf rule and connection type because socket and TCP rules differ.

For an application deployment, continue with the site’s Django, NGINX, Gunicorn and PostgreSQL setup. To check service state next, see how to list services in Ubuntu. If you are choosing a database for a small application, compare SQLite and PostgreSQL before settling on a server requirement.

Conclusion

Install the client when you only need to connect, and install the server when Ubuntu should host the database. Keep Linux accounts distinct from PostgreSQL roles, use a dedicated application role, and consult the PostgreSQL Ubuntu download instructions or Ubuntu Server PostgreSQL guide for package and authentication details.

FAQ

These answers clarify the client-only installation and the default local login.

How do I install only the psql client on Ubuntu?

Install the postgresql-client package family for your Ubuntu release. It provides psql without requiring a local PostgreSQL server.

Why does psql try to use my Linux username?

By default, psql uses the operating-system username as the database role and often uses the same name for the database. Specify the role with -U and database with -d, or create matching database objects.

Do I need to create a Linux user for a PostgreSQL role?

No. A PostgreSQL role is stored inside the database server. A matching Linux user is needed only for authentication methods such as peer that compare local operating-system and database identities.