PostgreSQL is an open-source database management system based on the SQL query language. System administrators often choose PostgreSQL for its free usage, high performance, and compatibility with almost any hardware platform. You can also easily create a PostgreSQL cloud database at Hostman.
This guide explains how to manage users in PostgreSQL, including creating and deleting users, setting access permissions, and practical use cases (e.g., creating backups). The steps described here are performed in the PostgreSQL shell.
To begin, switch to the postgres
user account:
su – postgres
If you encounter a permissions error, escalate privileges using:
sudo su
or simply:
su
Once logged in as the postgres
user, start the PostgreSQL shell:
psql -U postgres template1
Here, template1
is a default example database. Replace it with the name of your database as needed. The session will run under the postgres
account.
Before managing users, you can view a list of all PostgreSQL users:
SELECT * FROM pg_user;
To create a new user account in PostgreSQL, you'll define the user with a password and assign the necessary privileges. Additionally, you'll need to configure access in the pg_hba.conf
file.
Create a new user with a password using the PostgreSQL shell:
CREATE USER user123 WITH PASSWORD 'myPassword';
Alternatively, use the Linux command line:
createuser -P hostman_user
Assign the user access rights to a specific database:
GRANT ALL PRIVILEGES ON DATABASE "database1" TO hostman_user;
Activate the connection to the database:
\c database1
Grant the user permissions for all tables in the default schema public
:
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO "hostman_user";
The default schema is public
, but users can switch to and use other schemas.
For specific tables, assign permissions with:
GRANT ALL PRIVILEGES ON TABLE table1 IN SCHEMA public TO "hostman_user";
Once done, exit the shell:
\q
To enable connections for the new user, configure the pg_hba.conf
file. Open the file for editing. The path would depend on your operating system and the Postgres version.
Debian/Ubuntu:
nano /etc/postgresql/16/main/pg_hba.conf
CentOS:
vi /var/lib/pgsql/16/data/pg_hba.conf
Make sure to specify your Postgres version instead of 16 if it differs.
Add the following line to allow hostman_user
to connect from localhost using password authentication:
# IPv4 local connections:
host all hostman_user 127.0.0.1/32 md5
Place this line above the default configuration line:
host all all 127.0.0.1/32 ident
Restart the PostgreSQL service to apply the changes:
systemctl restart postgresql
Verify that the new user can connect to the database:
psql -U hostman_user template1 -h 127.0.0.1
To manage user permissions efficiently, start by creating a group role:
CREATE ROLE "myRole" NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION;
Assign the user hostman_user
to the newly created group:
GRANT "myRole" TO hostman_user;
Connect to the database:
\c database1
Then, assign permissions to the group myRole
for all tables in the public
schema:
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO GROUP "myRole";
You can easily modify a user's properties, such as their password. For example, to change the password of the postgres
user:
ALTER USER postgres PASSWORD 'password';
From the Linux command line, you can achieve the same with:
sudo -u postgres psql -U postgres -d postgres -c "ALTER USER postgres PASSWORD 'password'"
To remove a user:
DROP USER hostman_user;
Instead of removing the user entirely, you can restrict their permissions:
REVOKE ALL PRIVILEGES ON ALL TABLES IN SCHEMA public FROM "hostman_user";
PostgreSQL allows fine-grained control over permissions. For instance, instead of granting all privileges, you can allow specific operations:
GRANT SELECT, UPDATE, INSERT ON ALL TABLES IN SCHEMA public TO "hostman_user";
This example permits hostman_user
to read, edit, and add data but prohibits deletion. To assign permissions for a specific table:
GRANT ALL PRIVILEGES ON table_users TO "hostman_user";
This setup provides flexibility in granting tailored access to users or groups while maintaining database security and integrity.
We recommend using an account with minimal privileges to perform database backups securely. Follow these steps to create a read-only PostgreSQL user for backups.
Create a user with a password for backup purposes:
CREATE USER bkpuser WITH PASSWORD 'bkppasswd';
Here, bkpuser
is the username, and bkppasswd
is the password. Replace them with your desired values.
Enable the backup user to connect to the database:
GRANT CONNECT ON DATABASE database TO bkpuser;
Connect to the database:
\c database
Grant the necessary privileges for backup operations:
GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO bkpuser;
The example above uses the public
schema. Replace it with another schema if required.
For detailed instructions on backing up and restoring PostgreSQL databases using pg_dump
, check out this comprehensive guide.
This guide explains how to create PostgreSQL users via the console and configure their permissions. Tools like pgAdmin
can simplify these tasks for users who prefer graphical interfaces. To experiment with user management without risking production databases, consider testing on cloud servers like Hostman's.