PostgreSQL is a powerful, open source object-relational database system. It has more than 20 years of active development and a proven architecture that has earned it a strong reputation for reliability, data integrity, and correctness.
1. Install packages #
apt-get install postgresql postgresql-client
2. Create a user #
Switch to the postgres Linux user and create the database user:
su - postgres
createuser user1
Still as the postgres user, connect to the default template1 database and set a password for the new user:
psql template1
ALTER USER user1 PASSWORD 'password';
Leave the psql console with \q, then run exit to get back to the root user.
That’s actually all you need to install and create the first user. Follow the remaining steps to allow remote access to the PostgreSQL server.
3. Enable TCP/IP connections #
To let a remote client connect to the server, edit this line in the main config file /etc/postgresql/<version>/main/postgresql.conf (and remove the # at its start):
listen_addresses = '*'
Find the version number in the path with ls /etc/postgresql.
4. Allow connections from your IP address or range #
Add this line to /etc/postgresql/<version>/main/pg_hba.conf:
host all all 46.36.0.0/16 md5
Using this line I enabled the 46.36.0.0/16 IP range (netmask 255.255.0.0) to connect to the server. Make sure you allow as few IP addresses as possible.
Don’t use the trust method instead of md5. It would let anyone from the allowed range into the database without a password, as any user including the postgres superuser.
5. Restart PostgreSQL #
Changes in postgresql.conf take effect only after a restart:
systemctl restart postgresql
You’re all set. Make sure port 5432 is open in all firewalls to be able to manage the server remotely. To back up the database, see How to export PostgreSQL database.