Install PostgreSQL
By default, Ubuntu 16.04 instance has already postgresql package, but it is pretty out of date. You can still install latest version available (currently 9.6) by adding PostgreSQL repo into your ubuntu instance.
First create a file at /etc/apt/sources.list.d/postgres.list with this content
| |
Then run these commands to install PostgreSQL signing key and also install PostgreSQL 9.6
| |
Now PostgreSQL should already running on your machine. To check it you can use
| |
Create User and Database
Newly created PostgreSQL instance has a superuser called postgres without a password. Fortunately postgres user can only login from localhost so it should be secure by default. If you want to add password to postgres you can run
| |
It is better to have it’s own user and database for each applications. To create them you can use
| |
Allow Remote Connections
The PostgreSQL instance that have been installed will listen to localhost and only allow postgres user to connect inside the machine without password authentication. To enable remote connections, there are 2 things that need to be done.
First you must change PostgreSQL listen address to your machine ip, for example your machine private ip. Open /etc/postgresql/9.6/main/postgresql.conf and find this line
| |
Change it into
| |
Then you also need to allow remote connections to access your PostgreSQL instance. To do that open /etc/postgresql/9.6/main/pg_hba.conf and add this line to the end of the file
| |
It will allows remote connections from anywhere (notice 0.0.0.0/0) to access db_name with user user_name. You can improve the security by changing 0.0.0.0/0 to specific address, like 192.168.1.20/32. Restart the PostgreSQL instance by sudo systemctl restart postgresql then your remote clients should be able to connect to your PostgreSQL instance.