The simplest supported installation is sudo apt update followed by sudo apt install postgresql. Ubuntu supplies a PostgreSQL server package for every Ubuntu release, creates a usable cluster and service, and integrates updates with normal Ubuntu patch management. Use the PostgreSQL Global Development Group (PGDG) APT repository instead when your application requires a different supported major version than the one bundled with your Ubuntu release.
Need a server to try this on? Antihost offers Linux VPS plans with NVMe storage, full root access and a 14-day free trial.
After installation, administer the local server through the operating-system postgres account, keep Ubuntu’s peer authentication for local administration unless your design requires otherwise, and use narrowly scoped hostssl rules with SCRAM authentication for remote clients.
As an Amazon Associate I earn from qualifying purchases.
Choose the installation path before running commands
Ubuntu and Debian normally ship one PostgreSQL server major version per operating-system release. The right installation method therefore depends on whether the version selected by Ubuntu meets your application, extension, and migration requirements.
| Consideration | Ubuntu repository | PGDG repository |
|---|---|---|
| Version selection | Uses the PostgreSQL major version packaged for your Ubuntu release and keeps that release’s supported snapshot. | Lets you install a chosen supported major version, such as postgresql-18, when Ubuntu’s bundled version is different. |
| Patch integration | Updates arrive through normal Ubuntu package and security-maintenance workflows. | Packages are delivered through APT by PGDG and follow PGDG’s supported builds. |
| Operational effort | One package installation after refreshing APT metadata. | Requires repository setup, signing-key handling performed by the setup script, and checks for Ubuntu codename and architecture support. |
| Best fit | New servers where the distribution version is compatible with the application. | Applications that explicitly require another supported major version or need a version-selection policy independent of the Ubuntu release. |
Do not choose PGDG merely because it offers a newer number. A second repository adds another packaging source to maintain. Choose it when a compatibility requirement, upgrade plan, or supported extension matrix justifies that complexity.
#1 Best Overall
Check the Ubuntu server first
Log in with an account that can use sudo. Confirm the Ubuntu release and codename before selecting a repository path. The codename matters for PGDG because repository support and available architectures vary by release.
- Record whether the server is Ubuntu 22.04 (jammy), 24.04 (noble), 26.04 (resolute), or 26.10 (stonking), all of which are listed among PGDG’s currently supported Ubuntu releases at the time of this guide.
- Decide which PostgreSQL major version your application supports. Treat the major number as a compatibility decision, not simply as a request for the newest package.
- Make sure the server can reach Ubuntu’s package infrastructure, or the PGDG repository if you will use it.
- Plan whether clients will connect only locally or from an application network. Authentication and network scope should be designed before opening remote access.
Also record the machine architecture and the repository source in your change notes. PGDG architecture coverage is not identical for every Ubuntu release, so a package name that works on one server is not proof that it is available on another. If the required major version is available from Ubuntu, staying with one distribution source generally keeps package discovery and maintenance simpler.
Install the version supplied by Ubuntu
For most servers, this is the shortest and least surprising route. Ubuntu’s PostgreSQL package is available in all Ubuntu versions and is configured with viable defaults when installed.
- Refresh package metadata:
sudo apt update - Install the server package:
sudo apt install postgresql
The package creates the PostgreSQL cluster used by the administrator and installs a system service. Ubuntu’s packaged configuration is intended to be usable immediately, while still allowing you to customize it for your workload.
The unversioned Ubuntu package name is deliberate: it follows the PostgreSQL major version selected for that Ubuntu release. It is therefore a good default for a new server when the application supports the distribution version, but it is not a request for a particular major number. Check the installed result before restoring data or installing version-specific extensions.
Verify the service and client
Check that the service is present and running:
sudo systemctl status postgresql.service
Check the installed client version:
psql --version
The exact major version is the important part of the output. Record it with your server build notes so that future migrations and extension installations target the same series. A successful package transaction is not a substitute for checking service status; resolve a failed service before proceeding to authentication tests.
Install a chosen major version from PGDG
Use PGDG when Ubuntu’s release package does not meet a documented version requirement. PGDG currently lists support for Ubuntu jammy 22.04, noble 24.04, resolute 26.04, and stonking 26.10, with architecture availability differing by release. Confirm that your specific codename and architecture are supported before proceeding.
Set up the repository
- Install the repository-management package:
sudo apt install -y postgresql-common - Run PGDG’s documented setup script:
sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh - Refresh APT metadata after the repository has been added:
sudo apt update
The setup process handles the repository configuration and signing-key arrangement. Review its output for warnings rather than accepting a repository that does not match the server’s codename.
Install the required major version
Install a versioned package. For example:
sudo apt install postgresql-18
Replace 18 with the supported major version required by your application. PGDG also provides unversioned postgresql and postgresql-client meta-packages; those follow the latest supported version and therefore do not express a stay-on-this-major policy. Use a versioned package when predictable major-version selection matters.
Rank #2
Do not mix the decision about the server package with the client command found first on your PATH. A host can contain more than one client or cluster, especially after testing multiple major versions. Keep the selected major version in deployment documentation and use that record when configuring applications, extensions, and backup or restore procedures.
Verify which cluster and service were installed
After installation, check the service and client as you would with the Ubuntu package:
sudo systemctl status postgresql.service
psql --version
If more than one PostgreSQL major version is installed on the machine, do not assume that an unqualified psql command or generic service name identifies the cluster your application uses. Record the intended major version and cluster in your deployment documentation before configuring clients.
Make the first local connection
Ubuntu’s normal local-administration pattern is to become the operating-system postgres account for one command and connect to the default template database:
sudo -u postgres psql template1
This uses peer authentication: PostgreSQL checks the operating-system identity instead of asking for a database password. It is useful for initial administration because it avoids placing credentials in a command line or environment variable.
Free tools Windows power users keep installed
One-click scans. No signup required.
At the psql prompt, you can inspect the server and then leave with:
q
Do not confuse the Linux account named postgres with a password you must immediately set. A password is needed when a client will authenticate with a password, particularly over a host connection. Local peer access can continue to work without one.
Set a password for the database administrator only when needed
Connect locally as the operating-system postgres account, then run the documented SQL statement at the psql prompt:
ALTER USER postgres with encrypted password 'your_password';
Replace the example with a strong secret. The password is sensitive: do not paste a real value into documentation, tickets, screenshots, shell history, or source control. Run the statement interactively and protect the terminal and account from which you administer the server.
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchRank #3
A password alone does not make remote access work. PostgreSQL must also have a matching host-authentication rule, and the server must be configured and reachable for the intended network path. If local administration is the only requirement, leaving peer authentication in place avoids introducing an unnecessary password path.
Understand Ubuntu’s default authentication model
Current Ubuntu guidance uses different defaults for local and host connections:
- Local connections:
peerauthentication. The database checks the operating-system user, which is whysudo -u postgres psql template1works without a password. - Host connections:
scram-sha-256. A TCP client proves knowledge of the database role’s password using SCRAM.
These methods are selected by rules in pg_hba.conf. PostgreSQL’s cluster initialization writes that file according to the selected authentication method, so authentication is part of the server design rather than an afterthought.
Think of a local socket connection and a host connection as separate authentication paths. A password set for the role does not override a matching local peer rule, and a remote client does not use peer authentication merely because it is on the same private network. The first matching line in the host-based file controls the result.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Allow remote connections securely
Remote access should be limited to the database, role, network, and transport that the application actually needs. The relevant host-based rules are stored under:
/etc/postgresql/*/main/pg_hba.conf
Write a narrow SSL-enforced rule
Ubuntu documents this pattern:
hostssl template1 postgres 192.168.122.1/24 scram-sha-256
Each field has a security consequence:
hostsslmatches TCP connections that use SSL and rejects non-SSL TCP attempts for that rule.template1is the database name in the example. Replace it with the database the client actually needs; do not grant access to every database unless that is intentional.postgresis the role in the example. Prefer a dedicated application role with only the required privileges rather than using the administrative role for application traffic.192.168.122.1/24is an example CIDR range. Replace it with the real, smallest application network. Copying this address range unchanged could expose the wrong clients or fail to match your network.scram-sha-256requires password authentication using the role’s SCRAM verifier.
Place the rule deliberately among the existing entries. PostgreSQL evaluates matching rules in order, so an earlier rule can determine the result before a later rule is reached. Keep the file readable and document why each non-default rule exists.
Use the database and role names from the actual application, not necessarily the example’s template1 and postgres. Restricting those fields makes an accidental client configuration easier to detect and prevents a rule intended for one workload from silently matching another.
Apply the change
After changing pg_hba.conf or another server setting, restart PostgreSQL:
Recommended Free Tools
sudo systemctl restart postgresql.service
A restart makes the new authentication and server configuration active. Schedule it when a brief connection interruption is acceptable, and verify service status immediately afterward.
Test from a client
Ubuntu documents this client-side form:
psql --host your-servers-dns-or-ip --username postgres --password --dbname template1
Use the real DNS name or address, role, and database. The command prompts for the password instead of putting it in the command itself. Test from an address inside the CIDR range you allowed, and confirm that a client outside that range is rejected. If SSL is mandatory, verify that the connection is using the hostssl rule rather than a broader non-SSL rule.
Configuration and cluster facts that prevent common mistakes
A package install normally initializes the cluster
For a standard Ubuntu or PGDG package installation, the cluster is created for the administrator as part of installation. You normally do not need to run initdb manually just to obtain a working single-server installation.
Authentication belongs in the cluster design
initdb creates pg_hba.conf entries based on the authentication method selected during initialization. If you are building a custom layout, deciding on peer, SCRAM, SSL requirements, and allowed networks before exposing the service avoids an insecure temporary configuration.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Fix the driver behind crashes, sound loss and screen glitches3Repair Windows errors before they cause bigger problemsDo not mix version assumptions
Ubuntu’s package and PGDG’s versioned package can produce different major versions on different Ubuntu releases. Before restoring a database, installing extensions, or pointing an application at the server, verify the actual server version and the cluster that owns the target database.
Keep installation records
Write down the Ubuntu release and codename, architecture, PostgreSQL major version, package source, and cluster selected. These details explain later differences between otherwise similar servers and make a rebuild repeatable. They also prevent an operator from replacing a versioned PGDG package with an unversioned meta-package by accident.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Troubleshooting installation and connection failures
apt install postgresql installs an unexpected major version
This is expected when using Ubuntu’s repository: Ubuntu selects one PostgreSQL server snapshot for the lifetime of each Ubuntu release. If the application requires another supported major version, use the PGDG path and install a versioned package such as postgresql-18 instead of relying on the unversioned meta-package.
The PGDG setup script does not offer the server’s release
Recheck the Ubuntu codename and architecture. PGDG’s support list and architecture coverage vary by release. Do not substitute a different codename in repository configuration; use a supported release or remain with Ubuntu’s package.
Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Repair Windows errors before they cause bigger problemsFix Now →Local psql asks for a password
The command may not be running as the operating-system postgres user, or an earlier pg_hba.conf rule may be changing local authentication. Start with sudo -u postgres psql template1 and inspect the local rules before changing passwords.
A remote password is rejected
Confirm that the role has a password, the client is using the intended database and role, and the source address falls inside the allowed CIDR. Then inspect rule order in pg_hba.conf. A rule for another database, role, or network will not match this connection.
A remote connection is rejected before authentication
Check that the client is using the server’s reachable DNS name or address, that the connection is using SSL when the rule is hostssl, and that PostgreSQL was restarted after the configuration change. If the service status is not healthy, resolve that service failure before changing authentication rules again.
A configuration edit appears to have no effect
Restart with:
sudo systemctl restart postgresql.service
Then repeat the connection test and confirm that you edited the pg_hba.conf belonging to the active cluster and major version, not a file for an unused installation.
The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Operational checklist after installation
- Record the Ubuntu release, codename, PostgreSQL major version, and installation source.
- Confirm that
postgresql.serviceis running. - Verify local administration with
sudo -u postgres psql template1. - Set the
postgrespassword only if a password-authenticated client requires it, and store the secret securely. - Keep local peer authentication unless there is a specific reason to change it.
- For remote clients, use a dedicated role, the required database, a narrow source CIDR, SCRAM, and SSL-enforced
hostsslrules. - Restart PostgreSQL after authentication or server-setting changes.
- Test both an authorized client and an unauthorized network path so that the rule’s scope is proven rather than assumed.
Frequently Asked Questions
Should I use Ubuntu’s PostgreSQL package or PGDG?
Use Ubuntu’s package when the major version selected for your Ubuntu release meets the application’s compatibility requirements. Choose PGDG when you need a different supported major version. PGDG offers more version control, but it adds repository, signing-key, codename, and architecture checks.
What does sudo apt install postgresql install?
It installs Ubuntu’s PostgreSQL server package, creates a usable default cluster and service, and follows the major version packaged for that Ubuntu release. It does not guarantee the newest PostgreSQL major version. Check psql --version after installation and record the result.
Do I need to set a password for the postgres role immediately?
No. Local administration normally uses peer authentication, so sudo -u postgres psql template1 works without a database password. Set a password when a password-authenticated host connection requires it, and never place the real secret in shell history or documentation.
How do I allow a remote application to connect securely?
Set a password for the required role, add a narrowly scoped hostssl rule in /etc/postgresql/*/main/pg_hba.conf, and use scram-sha-256. Limit the rule to the actual database, role, and smallest application CIDR. Restart PostgreSQL, then test from an authorized client and an address outside the permitted range.
Why does a remote connection still fail after I set the password?
A password alone is insufficient. Check the database and role in the matching rule, the client’s source CIDR, rule order, SSL usage, server reachability, and whether PostgreSQL was restarted after editing the configuration. Also verify that the client is reaching the intended cluster and major version.
Why did PGDG install a different version than I expected?
The unversioned PGDG packages are meta-packages that follow the latest supported version. Install a versioned package such as postgresql-18, replacing 18 with the required supported major version, when you need a predictable major-version policy.
The Bottom Line
Start with Ubuntu’s postgresql package unless your application requires another major version. Use PGDG and a versioned package when that requirement is real. After installation, verify the service and version, administer locally through peer authentication, set passwords only when needed, and expose remote access only through narrowly scoped SCRAM and SSL-enforced hostssl rules.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




