Local superuser in PostgreSQL

Understand what the PostgreSQL local superuser is, how it is created during installation, and how to connect, configure, and use it safely on your development machine.

Local superuser in PostgreSQL

When you install PostgreSQL, the installer does something that often surprises beginners: it creates a database user account for you, gives that account unlimited power over the database server, and then sometimes never asks you to choose a password for it. That account is called the local superuser, and on almost every PostgreSQL installation it has the name postgres.

You do not need to be a database expert to work with this account, but you do need to understand what it is, where it lives, how to connect to it, and why you should be careful with it. This page covers all of that using the PostgreSQL server you installed on your own machine.


What a superuser is

A database user is an account that can log in to the PostgreSQL server and perform actions such as creating tables, inserting rows, or running queries. A superuser is a database user that has every possible permission inside PostgreSQL. There is no "are you sure?" prompt for a superuser. If a superuser tells PostgreSQL to delete a database, the database is deleted. If a superuser wants to create a new user, read any table, change any setting, or shut the server down, PostgreSQL allows it immediately.

PostgreSQL uses the word role to refer to users and groups. A role can be allowed to log in, in which case it acts as a user account, or it can be prevented from logging in and used only to group other roles together. The default postgres account is a role that is allowed to log in and has the SUPERUSER attribute. When you hear "postgres user," "postgres role," or "postgres superuser," they all refer to the same account in different contexts.

Superuser is a PostgreSQL concept:

The PostgreSQL superuser is not the same thing as the administrator account on your operating system. On Linux, the postgres database user usually maps to a separate operating system user also called postgres, but PostgreSQL permissions and operating system permissions are two different layers. A database superuser can do anything inside PostgreSQL - and it can also read or write files anywhere the server's operating-system user has access, for example with COPY ... TO a file or COPY ... TO PROGRAM, which runs an operating-system command. That is exactly why superuser credentials must stay out of application code.

A mental model that helps many beginners: think of PostgreSQL as a building. Regular database users have keys to specific rooms. The database owner has keys to one entire floor. The postgres superuser is the building manager with a master key, the alarm codes, the maintenance logs, and the authority to evict anyone, including the owner.


Why PostgreSQL creates a superuser during installation

PostgreSQL is not an application that you simply double-click and use. It is a database server: a background program that manages data files on disk and accepts connections from client tools. When the server starts for the first time, something has to initialize the storage structure, create the system databases, and establish the first login account. That first account must have enough power to create all future accounts and databases. If it did not, you could install PostgreSQL and find yourself locked out with no way to manage the server.

The installation process therefore creates one bootstrap superuser. This is a built-in role that PostgreSQL itself uses internally, but it is also the account you will use as the first administrator on a fresh installation.

On Linux, the installer typically creates an operating system user named postgres and then uses that OS user to run the PostgreSQL server process. On macOS, Homebrew and Postgres.app run the server as your own user account instead, so there is no postgres operating system user and no postgres database role by default. On Windows, PostgreSQL runs as a Windows service, and the installer asks you to provide a password for the postgres database superuser during installation. This is why a Windows user usually knows their superuser password, while a Linux user often discovers that no password was ever set.

The password situation differs by platform:

On a standard Ubuntu or Debian installation, the default postgres role is configured to use peer authentication. That means you can connect as postgres only from the matching operating system user, and no password is used by default. On macOS with Homebrew there is no postgres role at all by default - the superuser is named after your macOS username - and local connections use trust authentication, so no password is needed either. On Windows, the graphical installer asks you to type a password for the postgres superuser. If you install PostgreSQL on Windows and later forget that password, you cannot simply leave it blank and connect.


Where the local superuser lives

The superuser is not stored in a file you can open with a text editor. PostgreSQL stores all roles, including postgres, inside the database cluster itself. The cluster here does not mean multiple computers. In PostgreSQL vocabulary, a cluster is one directory on disk that contains all the data files and metadata for one PostgreSQL server instance. When you installed PostgreSQL, the installer created a cluster, initialized the system tables, and wrote the postgres role into those tables.

When you start the PostgreSQL server, it reads the cluster directory, loads the role information, and is ready to accept logins. You do not edit a users.txt file to manage roles. Instead, you connect to PostgreSQL and run SQL commands such as CREATE ROLE, ALTER ROLE, or DROP ROLE. For the superuser account specifically, you will mostly use ALTER ROLE to set or change its password.

The exact location of the cluster depends on how you installed PostgreSQL. On Ubuntu, the default cluster data usually lives under /var/lib/postgresql/18/main or a similar path matching your PostgreSQL version. On macOS with Homebrew, it often lives under /opt/homebrew/var/postgresql@18. On Windows, the installer places it inside the data folder under your PostgreSQL installation directory, such as C:\Program Files\PostgreSQL\18\data. You do not need to open or edit these directories for anything covered on this page, but knowing that the cluster is just a folder helps demystify what "local" means: the superuser belongs to the PostgreSQL server running on your machine, not to some remote service.


A basic starter example - connecting as the postgres user

The first skill to learn is opening a connection to your local PostgreSQL server as the postgres superuser. PostgreSQL includes a command-line client called psql that lets you type SQL statements interactively. Every full PostgreSQL installation includes psql, so if you installed the server, this tool should already be available.

The connection procedure differs depending on your operating system because the default authentication method differs. Before trying any command, open a terminal window. On Windows, use the SQL Shell (psql) application that the PostgreSQL installer created, or open PowerShell and run psql from there if it is on your PATH.

On Linux, the typical first connection attempt looks like this:

sudo -u postgres psql

The command says: "Use sudo to run the program psql as the operating system user named postgres." The PostgreSQL server sees the connection coming from the postgres operating system user, and because peer authentication is configured by default, it grants immediate access to the matching postgres database role. No password prompt appears.

If the command works, you will see something like this:

psql (18.7 (Ubuntu 18.7-1.pgdg24.04+1))
Type "help" for help.
postgres=#

The line postgres=# is the psql prompt. The first part before = tells you which database you are connected to, and the # indicates that you are connected as a superuser. A regular user would see a > symbol instead. When you see postgres=#, you are talking directly to the local PostgreSQL superuser.

On Windows, open SQL Shell (psql). It will prompt you for the server name, database name, port, and username. Accept the defaults for server, database, and port by pressing Enter. For username, type postgres. Then enter the password you chose during installation. If successful, the same postgres=# prompt appears.

Confirming the superuser connection:

If your prompt shows postgres=# with the hash symbol, you have connected as a true PostgreSQL superuser. This is the normal starting state after a fresh install, and it means the installation created the default superuser role correctly.

To leave psql, type \q and press Enter. This returns you to your normal terminal prompt.


The default postgres database

When you first connect, you will be inside a database that is also called postgres. It is easy to confuse three different things that share this name:

  • The operating system user named postgres, which exists on Linux and macOS.
  • The database role named postgres, which is the superuser account.
  • The database named postgres, which is a default database used for administrative connections.

The database named postgres exists on every PostgreSQL installation. It is a convenient place to connect when you are performing maintenance tasks, creating other databases, or managing roles. You can also create your own databases later and connect to those instead, but on a fresh install, postgres is the only obvious target.

When you run sudo -u postgres psql on Linux, the psql tool automatically tries to connect to a database with the same name as the connecting user. Since you are connecting as the role postgres, it looks for a database called postgres, finds it, and connects. That is why the basic command works with no extra arguments.


Common connection scenarios

Several different commands will get you into a PostgreSQL session, depending on what you want to do. Here are the most useful ones and when to use each.

Connect to the default database as the postgres superuser on Linux:

sudo -u postgres psql

This is the simplest option for administrative work on a machine using peer authentication. It gives you a superuser session in the postgres database.

Connect to a specific database as the postgres superuser:

sudo -u postgres psql -d myapp_db

Replace myapp_db with the name of a database that already exists. This is useful when you want to inspect or modify a project database while still using superuser permissions. The -d flag stands for "database."

Connect using a password instead of the operating system user on Linux:

psql -U postgres -h localhost -d postgres

The -U flag sets the username, and -h localhost forces a TCP connection. Depending on how pg_hba.conf is configured on your machine, PostgreSQL may then prompt for a password. If no password has been set yet for the postgres role, this command will fail even if you type the correct operating system password. This is a frequent source of confusion, and the fix is covered later in the page.

Connect on Windows:

psql -U postgres -h localhost -d postgres

This works the same as the Linux version. Windows PostgreSQL installations commonly require a password, so expect a prompt.

Username and database name are not interchangeable:

If you receive an error like database "admin" does not exist, it usually means you connected as the right user but PostgreSQL is trying to open a database with the same name as your operating system username. Specify the database explicitly with -d postgres to avoid this.


How PostgreSQL decides who can connect

When PostgreSQL receives a connection request, it does not simply trust the username provided. It looks at the connection type, the requested database, the requested role, and the IP address, and then checks those details against a configuration file called pg_hba.conf. The name stands for "PostgreSQL host-based authentication." This file defines the authentication rules for the entire server.

A beginner does not need to become an expert in pg_hba.conf to use the local superuser, but understanding the two authentication methods most often applied to the postgres account will prevent a lot of confusion.

Peer authentication on Unix systems

Peer authentication means: "If the operating system user matches the requested database role, allow the connection without a password." When you run sudo -u postgres psql, the OS identifies you as the user postgres, and PostgreSQL lets you in as the database role postgres. The connection is local, the identities match, and no password is needed.

This is the default for local connections on many Debian-based Linux distributions. It is convenient for administrators because they can always get in as long as they can run sudo on the machine.

Trust authentication

Trust authentication means: "Allow the connection without checking a password or an operating system identity." Some macOS Homebrew setups and some older local configurations use trust for local connections. If your system uses trust, the command psql -U postgres will work without any password prompt, even if you are logged in as a different OS user.

The difference matters when you later connect from an application. An application that connects over TCP with a username and password will not be subject to peer authentication, because peer only applies to local Unix socket connections. The application will need an actual password or another matching rule in pg_hba.conf.

Password authentication

Password authentication does what the name suggests: PostgreSQL asks for a password and verifies it against the stored role information. On Windows, this is the most common setup for the postgres role right after installation. On Linux, you may have to explicitly set a password before password authentication can work, because the role may have been created with no password.

You can inspect the rules if a connection fails:

If a connection attempt fails with a message like "no pg_hba.conf entry for host" or "Peer authentication failed," the cause is almost always a mismatch between your connection method and the rules in pg_hba.conf. Changing your connection command is often simpler than editing the configuration file.


Setting a password for the postgres superuser

On Linux and macOS, the postgres role often has no password immediately after installation. This is safe as long as your PostgreSQL server accepts only local connections, but it becomes a problem when a GUI tool such as DBeaver or a Rust application using SQLx tries to connect with a username and password. Those tools do not normally use operating system peer authentication; they connect over TCP and expect a password.

The fix is to set a password for the postgres role. You must already be connected to PostgreSQL as a superuser to do this. Use the connection method that works on your machine, then run the following SQL command:

ALTER ROLE postgres WITH PASSWORD 'choose_a_strong_password';

Replace choose_a_strong_password with a real password. PostgreSQL does not enforce any specific password complexity rules by default, but choosing a short or obvious password defeats the purpose, especially once the server accepts connections from outside your machine.

After running the command, PostgreSQL will store the password in encrypted form inside the cluster's system tables. You will not see the plain password again through normal PostgreSQL tools. To verify that the password is set, exit psql and try connecting with a username and host:

psql -U postgres -h localhost -d postgres

PostgreSQL should now prompt for a password. Type the one you just set. If the prompt appears and accepts your input, the superuser role now has a working password for TCP connections.

Do not set a blank password for convenience:

A blank password is not the same as "no password" in PostgreSQL. Depending on your pg_hba.conf rules, a blank password may still be rejected, or worse, it may be accepted and create a serious security hole. Always set a real password if your server listens on a network interface.


Changing an existing superuser password

If you already have a password for the postgres role but want to change it, the process is identical to setting one for the first time. Connect as the superuser through whichever method works, then run:

ALTER ROLE postgres WITH PASSWORD 'new_strong_password';

This command overwrites the previous password. There is no built-in password history, so you can rotate back to an old password if you choose. The change takes effect for all future connections immediately; existing connections are not interrupted.

On Windows, if you forget the existing password and cannot log in, the ALTER ROLE command will not help you because you need to connect before running it. Recovery from a lost Windows superuser password requires editing pg_hba.conf to temporarily use trust authentication, restarting the PostgreSQL service, connecting without a password, changing the password, and then restoring the original authentication rule. That process is a common task in PostgreSQL administration but falls outside the simple setup covered here.


Creating a second superuser role

The postgres account is the default superuser, but PostgreSQL does not limit you to a single superuser. You can create additional roles with the SUPERUSER attribute. This is sometimes useful when multiple developers share a development server, or when you want a clear separation between a maintenance account and the built-in default.

Connect as the postgres superuser first, then run:

CREATE ROLE dev_admin WITH LOGIN SUPERUSER PASSWORD 'dev_admin_password';

This statement creates a role named dev_admin, gives it permission to log in, marks it as a superuser, and sets its password. The role is now fully independent from postgres.

To confirm the new role exists and has the expected attributes, run:

\du

The \du command is a psql meta-command that lists all roles in the cluster. Your output will look similar to this:

                                   List of roles
 Role name  |                         Attributes
------------+------------------------------------------------------------
 postgres   | Superuser, Create role, Create DB, Replication, Bypass RLS
 dev_admin  | Superuser

Both roles appear as superusers. You can log out of psql and connect as dev_admin the same way you would connect as postgres:

psql -U dev_admin -h localhost -d postgres

The new superuser can create databases, delete roles, change server settings, and perform every action the postgres role can.

Superuser is not automatically granted to new roles:

The default privileges in PostgreSQL do not make every new role a superuser. A role created with CREATE ROLE some_name WITH LOGIN PASSWORD 'secret' is a normal user with no special powers. You must add the SUPERUSER keyword explicitly if you want that level of access. This is an important safety feature.


Creating a development role without superuser powers

For most Axum and SQLx development work, you do not need a superuser at all. You need a regular role that can create databases, or even just connect to an existing database and run queries. Giving every project a superuser account is unnecessary and risky. A mistake in a migration script or a test suite could drop an entire database, and a superuser connection would not prevent it.

Here is a common setup for a local development role:

CREATE ROLE app_user WITH LOGIN PASSWORD 'app_user_password';

This role can connect and execute normal SQL statements, but it cannot create new databases. If your development workflow uses SQLx's migration tools to create a database automatically, you may want to grant the ability to create databases:

ALTER ROLE app_user WITH CREATEDB;

The CREATEDB attribute lets the role create new databases, but it still does not make the role a superuser. A role with CREATEDB can create and drop the databases it owns, but it cannot read data from databases owned by other roles, cannot create other roles, and cannot change server-wide settings.

A good local development pattern:

Create one superuser account for administrative tasks, and a separate regular role with only LOGIN and CREATEDB for your application. Connect your Axum app to the regular role. If a bug in your code accidentally drops a table, the damage is limited to what that regular role owns, rather than giving the bug full control over the entire PostgreSQL server.


Understanding the postgres role vs your OS user

One of the most confusing parts of PostgreSQL for newcomers on Linux and macOS is the separation between the operating system user and the database role. When you run sudo -u postgres psql, you are not "becoming" the database user directly. You are running the psql program as the operating system user postgres. The PostgreSQL server then checks its authentication rules and maps that OS identity to the database role with the same name.

Your normal operating system account, such as alex or developer, has no automatic relationship to any database role unless someone creates one and configures peer authentication for it. If you run psql as your normal OS user without specifying a username, psql tries to connect as a database role with the same name as your OS user. On a fresh install, that role usually does not exist, so you see an error like:

psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL:  role "alex" does not exist

This does not mean your PostgreSQL installation is broken. It means you asked PostgreSQL for a role that has not been created yet. You can fix the immediate issue by connecting as postgres instead. Later, if you want your normal OS user to connect without typing a username, you can create a matching role and grant it the appropriate permissions.

Role names are case-sensitive in a surprising way:

In PostgreSQL, unquoted identifiers are folded to lowercase. A role created with CREATE ROLE MyUser is stored as myuser. If you must use uppercase or mixed-case names, you need double quotes both at creation and in every future reference. For simplicity, use all-lowercase role names during local development.


Practical walkthrough - securing a fresh Linux install

On a fresh Ubuntu or Debian PostgreSQL install, the typical situation is: the postgres superuser exists, peer authentication works, and no password is set. The server accepts connections only from the local machine by default, so the risk is low. Before using the database from GUI tools or a Rust application, most developers perform these steps.

1

Step 1: Connect using peer authentication

Open a terminal and connect to PostgreSQL as the operating system user postgres:

sudo -u postgres psql

You should see the postgres=# prompt.

2

Step 2: Set a password for the postgres superuser

Run this SQL command inside the psql session:

ALTER ROLE postgres WITH PASSWORD 'a_strong_local_password';

This enables password-based connections for tools that do not use peer authentication.

3

Step 3: Create a regular development role

Create a separate role for your application work:

CREATE ROLE app_user WITH LOGIN PASSWORD 'app_password';
ALTER ROLE app_user WITH CREATEDB;

The CREATEDB attribute is optional. Include it only if your workflow requires the application to create its own databases.

4

Step 4: Verify password connections

Exit psql with \q, then try connecting as the regular role over TCP:

psql -U app_user -h localhost -d postgres

If PostgreSQL prompts for a password and accepts it, your local authentication setup is working correctly.


Practical walkthrough - working with a Windows install

Windows users typically have a different starting point. The graphical installer asks for a postgres superuser password during installation, stores it, and configures pg_hba.conf to use password authentication for local TCP connections. The SQL Shell (psql) application is the easiest way to test the connection.

1

Step 1: Open SQL Shell (psql)

Find SQL Shell (psql) in the Start menu and open it.

2

Step 2: Accept defaults and enter the username

For Server, Database, and Port, press Enter to accept the defaults. At the Username prompt, type postgres and press Enter.

3

Step 3: Enter the superuser password

Type the password you chose during installation. The characters will not appear on screen. Press Enter. If the password is correct, the postgres=# prompt appears.

4

Step 4: Create an application role

Inside psql, create a regular development role:

CREATE ROLE app_user WITH LOGIN PASSWORD 'app_password';
ALTER ROLE app_user WITH CREATEDB;

Keep this password distinct from the superuser password.


What can go wrong

Several errors show up repeatedly when beginners work with the local superuser. Knowing what they mean and how to respond will save time.

"role postgres does not exist"

This error usually means PostgreSQL is not installed with the expected default configuration, or you are connecting to a different server than you think. If you installed PostgreSQL from source or used a custom cluster initialization, the bootstrap superuser may have a different name. You can find the actual superuser name by checking the cluster initialization settings or by connecting through whatever authentication method your pg_hba.conf allows and running \du.

"Peer authentication failed for user postgres"

You are attempting a peer-authenticated connection from an operating system user that does not match the database role. If you ran psql -U postgres as your normal OS user on Ubuntu, peer authentication refuses the connection. The fix is to use sudo -u postgres psql instead, or to switch that rule to password authentication if you need TCP access.

"password authentication failed for user postgres"

You connected over TCP or asked for a password, and PostgreSQL did not accept the password you provided. On Linux, the most common cause is that no password has ever been set for the role, not that you typed it wrong. The solution is to connect through peer authentication first and set a password with ALTER ROLE.

"database postgres does not exist"

Every standard installation creates a postgres database, so this error usually indicates a nonstandard cluster or a connection string that points at the wrong port or host. Check that the PostgreSQL service is running and that you are using the port configured during installation.

Avoid editing pg_hba.conf unless you understand the consequences:

Authentication configuration mistakes can lock you out of the server. Before editing pg_hba.conf, make a copy of the file, record the exact lines you change, and know how to restart the PostgreSQL service. A small syntax error in this file can prevent PostgreSQL from starting entirely.


Where the superuser fits into an Axum and SQLx project

When you build an Axum backend that talks to PostgreSQL, you will eventually need a connection string. That string looks something like this:

postgresql://app_user:app_password@localhost:5432/myapp_db

The part before the first colon is the username, the part after it is the password, and the part after @ is the host and port. The database name comes last. Notice that this example uses app_user, not postgres. In a development setup, that is the preferred choice.

When you create a database for the project, you can either create it manually using the superuser:

CREATE DATABASE myapp_db OWNER app_user;

Or, if your regular role has the CREATEDB attribute, your SQLx migration tool can create it. Either way, the superuser is the account you use for administrative setup, while the regular role is the account your application uses for everyday queries.

Do not put superuser credentials in application code:

Hard-coding the postgres superuser password into your Rust source or .env file means every bug, every debug log, and every accidental commit exposes maximum database power. Create a regular role for the application and keep superuser credentials out of the project directory entirely.


Summary

The local superuser is the bootstrap account that makes a fresh PostgreSQL installation usable. On most systems it is named postgres, and it has complete authority over the server. The way you connect to it differs by platform: Linux and macOS installations often rely on operating system peer authentication, while Windows installations require a password chosen during setup.

The most important practical takeaway is to use the superuser for administrative setup and then step back. Set a password for it, create a separate role with ordinary permissions for your Axum projects, and keep that superuser connection out of your application code. PostgreSQL's permission system exists to limit damage, and a superuser connection bypasses those limits entirely.

The next step after understanding the superuser is to create the databases and roles your application will actually use. With the superuser connection working, you are ready to initialize a project database and move into writing real SQL.