Setup PostgreSQL
Introduction
This guide will walk you through installing PostgreSQL on Ubuntu, creating users and databases, and configuring it for remote connections. I've made sure to provide clear steps that work on the latest Ubuntu versions.
Installing PostgreSQL
Update your package lists and install PostgreSQL:
Verifying Installation
Check that PostgreSQL is running properly:
You should see output with "active (running)" in green text.
Creating a Database and User
Log in as the PostgreSQL admin user
You'll now be at the PostgreSQL prompt (postgres=#
).
Create a new database
Create a new user (role) with a password
Grant privileges to the user
Exit the PostgreSQL prompt
Verify your new database and user
To check if everything was created correctly, log back in:
List all databases:
List all users:
You should see your new database and user in these lists.
Configuring Remote Access
By default, PostgreSQL only accepts connections from the local machine. To allow remote connections:
Edit the PostgreSQL configuration file
Find your PostgreSQL version first:
You'll see a version number like 12
, 13
, or 14
.
Now edit the configuration file (replace 14
with your version):
Find the line:
Change it to:
Save and exit (Ctrl+O, then Ctrl+X).
Configure client authentication
Edit the client authentication file:
Add these lines at the end of the file:
Note: For PostgreSQL 14+, you can use
scram-sha-256
instead ofmd5
for stronger password encryption.
Restart PostgreSQL to apply changes
Open the firewall for PostgreSQL
If you have a firewall enabled, allow connections to the PostgreSQL port:
Connection String Format
To connect to your PostgreSQL database from applications, use this connection string format:
Example:
Where:
username
: Your PostgreSQL userpassword
: Your user's passwordhostname
: Your server's IP address or domain nameport
: PostgreSQL port (default is 5432)database_name
: Your database name
Note: Some applications use
postgresql://
instead ofpostgres://
at the beginning.
Basic PostgreSQL Commands
Here are some essential PostgreSQL commands:
Connecting to a database
If you're on the local machine:
Inside psql:
\l
List all databases
\c database_name
Connect to a specific database
\dt
List all tables in the current database
\du
List all users (roles)
\d table_name
Describe a table
\?
Show help
\q
Quit psql
Troubleshooting
Can't connect remotely
Verify PostgreSQL is listening on all interfaces:
You should see
0.0.0.0:5432
in the output.If netstat is not available, install it:
Check your firewall settings:
Ensure the client authentication file (pg_hba.conf) contains the correct entry for remote connections.
Authentication failed
Double-check username and password
Ensure you've granted correct privileges to the user
Check if the user is allowed to connect from your client's IP address in pg_hba.conf
Database doesn't exist or permission denied
Make sure the database exists:
Check that your user has the necessary permissions:
Permission denied for schema public
Example database_url: postgres://root:gradient@127.0.0.1:5432/gradient
Commands for fixing:
Quick Reference: Common Commands
Service management
Database operations
Last updated