Tip: Ubuntu 18.04 is old. Newer Ubuntu versions use almost the same steps.
What is PostgreSQL?
A database is a place where programs store information, like a big filing cabinet. PostgreSQL, often called Postgres, is a free and popular database. Websites and apps use it to keep users, orders and posts.
Steps
- Log in to your server with SSH. SSH lets you type commands on a remote computer.
- Update the package list.
apt update - Install PostgreSQL and its extra tools.
apt install -y postgresql postgresql-contrib - Check that it is running.
systemctl status postgresql - Switch to the special
postgresuser. The install creates this user to manage the database.su - postgres - Open the Postgres command window.
The prompt changes topsqlpostgres=#. - Create a new database user. Change the name and password.
CREATE USER myuser WITH PASSWORD 'choose-a-strong-password'; - Create a database owned by that user.
CREATE DATABASE mydb OWNER myuser; - Leave the command window.
\q - Go back to the root user.
exit - Test the new login.
Type the password when asked. If you see thepsql -h 127.0.0.1 -U myuser -d mydbmydb=>prompt, it works.
Useful facts
- Postgres uses port 5432. A port is like a numbered door into your server.
- By default it only listens on the server itself. This is the safe choice.
- On Ubuntu 18.04, the version installed is 10.
- Settings live in
/etc/postgresql/10/main/.
Warning: Do not open port 5432 to the whole internet. If a remote app needs access, allow only its IP address and take a backup first.
Quick recap
- PostgreSQL is a free database.
- Install it with
apt install postgresql postgresql-contrib. - Use the
postgresuser andpsqlto create a user and a database. - Keep port 5432 private.