It's always good practice to update your package list before installing new software. Open a terminal and run:
sudo apt update
You can install PostgreSQL and its associated packages using the following command:
sudo apt install postgresql postgresql-contrib
This command installs the PostgreSQL server along with some useful additional packages (like postgresql-contrib).
After the installation is complete, you can verify that PostgreSQL is installed and running. Use the following command to check the status of the PostgreSQL service:
sudo systemctl status postgresql
You should see output indicating that the PostgreSQL service is active and running.
PostgreSQL creates a default user named postgres. You need to switch to this user to perform administrative tasks:
sudo -i -u postgres
Now, you are logged in as the postgres user.
To start using PostgreSQL, access its command line interface (CLI) by typing:
psql
This command will log you into the PostgreSQL prompt as the postgres user. You should see the PostgreSQL prompt:
postgres=#
To create a new database, use the following SQL command:
CREATE DATABASE mydatabase;
Replace mydatabase with your desired database name.
You can create a new PostgreSQL user with the following command:
CREATE USER myuser WITH PASSWORD 'mypassword';
Replace myuser with your desired username and mypassword with a strong password.
Grant the new user privileges on the new database:
GRANT ALL PRIVILEGES ON DATABASE mydatabase TO myuser;
Replace mydatabase and myuser with your database name and username, respectively.
To exit the PostgreSQL prompt, type:
\q
To ensure everything is set up correctly, you can switch to the new user and connect to the new database. First, exit the postgres user:
exit
Then, connect to the new database using the new user:
psql -U myuser -d mydatabase
Replace myuser and mydatabase with your username and database name. You will be prompted to enter the password for the new user.
By default, PostgreSQL only allows connections from the localhost. To allow remote connections, you need to configure PostgreSQL to listen on all IP addresses and modify the host-based authentication (HBA) file.
Open the postgresql.conf file:
sudo nano /etc/postgresql/12/main/postgresql.conf
Find the line that starts with #listen_addresses and change it to:
listen_addresses = '*'
Save and close the file.
Open the pg_hba.conf file:
sudo nano /etc/postgresql/12/main/pg_hba.conf
Add the following line to allow connections from any IP address:
host all all 0.0.0.0/0 md5
Save and close the file.
For the changes to take effect, restart the PostgreSQL service:
sudo systemctl restart postgresql
For secure remote connections, it's recommended to enable SSL. Here's a brief overview:
1. Generate SSL Certificates: Create a server key and certificate.
2. Configure PostgreSQL to Use SSL: Edit the postgresql.conf file to point to the SSL certificate and key files.
3. Restart PostgreSQL: Apply the changes by restarting the PostgreSQL service.
Refer to the PostgreSQL documentation for detailed steps on enabling SSL.
Jorge García
Fullstack developer