Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124

PostgreSQL, commonly known as Postgres, is a database system that uses SQL to organize and manage information. It sticks to standard rules and offers strong features, including dependable transactions and support for many users working at the same time without getting in each other’s way.
This guide walks you through the basics of getting Postgres running on an Ubuntu server. You’ll learn how to install PostgreSQL and how to create a new user and database so you can start working with it right away.
To follow along with this tutorial, you will need one Ubuntu server. I am using Ubuntu 24.04 LTS.
System Requirements: PostgreSQL will run with as little as 1GB of RAM and about 1GB of disk space for simple use. If you’re planning to use it for real, day-to-day work, it’s better to have at least 4GB of RAM and an SSD for faster storage.
To get PostgreSQL installed, start by updating your server’s list of available packages.
sudo apt update
Next, install the main PostgreSQL package along with the -contrib package, which includes a set of helpful add-ons. The postgresql-contrib package gives you extra modules such as pg_stat_statements for tracking queries, uuid-ossp for creating UUIDs, and hstore for storing key-value data.
sudo apt install postgresql postgresql-contrib
When the system asks you to confirm the installation, type Y. If it also asks whether you want to restart any services, just press ENTER to go with the default option and move on.
Once the installation is complete, you’ll want to make sure PostgreSQL is up and running. This is also a good time to confirm which version of Postgres is installed.
sudo systemctl status postgresql
You should see an output similar to below indicating that the service is active and running.
● postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; preset: enabled)
Active: active (exited) since Mon 2025-12-01 17:12:09 UTC; 2min 30s ago
Main PID: 15265 (code=exited, status=0/SUCCESS)
CPU: 2ms
Dec 01 17:12:09 vmzilla01 systemd[1]: Starting postgresql.service - PostgreSQL RDBMS...
Dec 01 17:12:09 vmzilla01 systemd[1]: Finished postgresql.service - PostgreSQL RDBMS.
To check the PostgreSQL version:
sudo -u postgres psql -c "SELECT version();"
Output:
akhil_u@vmzilla01:~$ sudo -u postgres psql -c "SELECT version();"
version
----------------------------------------------------------------------------------------------------------------------------------------
PostgreSQL 16.10 (Ubuntu 16.10-0ubuntu0.24.04.1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0, 64-bit
(1 row)
PostgreSQL uses a system called “roles” to manage user access and permissions. Think of roles as being similar to traditional Unix or Linux users and groups.
When you install Postgres, it uses ident authentication by default. This links Postgres roles to matching Unix or Linux system accounts. In other words, if a role exists in Postgres, a system user with the same name can log in using that role.
During installation, Postgres creates a default user account called postgres, which is tied to the default Postgres role. You can use this account to access your database in several ways. One common method is to switch to the postgres user on your server by running the following command:
sudo -i -u postgres
After switching to the postgres user, you can open the PostgreSQL prompt by running the psql command in your terminal. This gives you direct access to the PostgreSQL interface, allowing you to start managing databases, running queries, and exploring your data immediately.
When you’re finished, you can exit the PostgreSQL prompt by typing \q. This will return you to the regular postgres Linux command line, ready for your next task. Example:
akhil_u@vmzilla01:~$ sudo -i -u postgres
postgres@vmzilla01:~$ psql
psql (16.10 (Ubuntu 16.10-0ubuntu0.24.04.1))
Type "help" for help.
postgres=# \q
postgres@vmzilla01:~$
Another method to access the PostgreSQL prompt is by running the psql command with sudo as the postgres user.
sudo -u postgres psql
This approach lets you log in to Postgres directly, skipping the intermediate bash shell, so you can start working with your databases right away.
PostgreSQL offers a variety of authentication options, including ident, md5, scram-sha-256, and peer. By default, Postgres uses the ident method, which provides reliable security, especially for single-user setups. These options give you flexibility to choose the best authentication approach for your database environment.
Creating user roles in PostgreSQL is an essential step for managing access and permissions in your database. Roles act like accounts with specific privileges, allowing you to control who can read, write, or manage your data.
To create a new role, start by logging into the PostgreSQL prompt using the sudo -u postgres psql command. Once you’re in, you can use the CREATE ROLE statement. For example:
CREATE ROLE username WITH LOGIN PASSWORD 'your_password';
This command creates a new role that can log in and sets a secure password. You can also assign additional privileges to the role depending on your needs. For instance, if you want the role to create databases, you can add:
ALTER ROLE username CREATEDB;
By carefully setting up roles, you ensure that each user has the right level of access while keeping your PostgreSQL database secure. Regularly reviewing and managing roles helps maintain proper database governance and prevents unauthorized access.
Alternatively, you can create a PostgreSQL role interactively directly from the Linux terminal using the following command:
sudo -u postgres createuser --interactive
The script will prompt you with some choices and, based on your responses, execute the correct Postgres commands to create a user to your specifications.
Output
Enter name of role to add: username
Shall the new role be a superuser? (y/n) y
Setting up a new database in PostgreSQL is a fundamental task for organizing and managing your data efficiently. PostgreSQL allows you to create multiple databases, each with its own set of tables, roles, and permissions.
Using the PostgreSQL Prompt:
After logging into the PostgreSQL prompt with the command, you can create a database with the sudo -u postgres psqlCREATE DATABASE statement:
CREATE DATABASE mydatabase;
This command creates a new database named mydatabase. You can also assign an owner to the database, which determines who has full control over it:
CREATE DATABASE mydatabase OWNER username;
Using the Linux Terminal:
Alternatively, you can create a PostgreSQL database directly from the Linux terminal using the createdb command:
sudo -u postgres createdb mydatabase
This method is quick and doesn’t require opening the PostgreSQL prompt.
Best Practices:
Creating databases properly ensures a secure and well-structured PostgreSQL environment, making it easier to manage users, roles, and data efficiently.
Installing PostgreSQL on Ubuntu is quick and straightforward, whether you use the terminal or the PostgreSQL prompt. By setting up roles and databases properly, you can manage users, control access, and keep your data secure. With these basics in place, you’re ready to start exploring Postgres and building a reliable, well-organized database environment.