Postgresql 16: Difference between revisions

From Alpine Linux
(Created page with "= General = PostgreSQL is a well known opensource database that scales well and is easy to use. in Alpine v3.20 we can install the latest version using the package postgresql16 = Install = {{Cmd|apk add postgresql16 postgresql16-contrib postgresql16-openrc}} {{Cmd|rc-update add postgresql}} {{Cmd|rc-service postgresql start}} This will start the postgresql 16 server and perform some initial configuration. = Configure = login as the postgres user and start psql to cr...")
 
m (Fix restart command)
 
(9 intermediate revisions by 3 users not shown)
Line 1: Line 1:
= General =
PostgreSQL is a well known opensource database that scales well and is easy to use. In Alpine v3.20 we can install the latest version using the package postgresql16
PostgreSQL is a well known opensource database that scales well and is easy to use. in Alpine v3.20 we can install the latest version using the package postgresql16


= Install =
== Installation ==
{{Cmd|apk add postgresql16 postgresql16-contrib postgresql16-openrc}}
{{Cmd|apk add {{pkg|postgresql16|arch=}} {{pkg|postgresql16-contrib|arch=}} {{pkg|postgresql16-openrc|arch=}}}}
{{Cmd|rc-update add postgresql}}
{{Cmd|rc-update add postgresql}}
{{Cmd|rc-service postgresql start}}
{{Cmd|rc-service postgresql start}}


This will start the postgresql 16 server and perform some initial configuration.  
This will start the postgresql 16 server and perform some initial configuration.


= Configure =
== Configuration ==
login as the postgres user and start psql to create a new user and database:
 
Login as the postgres user and start psql to create a new user and database:
{{Cmd|su postgres}}
{{Cmd|su postgres}}
{{Cmd|psql}}
{{Cmd|psql}}
{{Cmd|create user myuser with encrypted password 'mypass';}}
{{Cmd|create user user with encrypted password 'password';}}
{{Cmd|create database mydb;}}
{{Cmd|create database database;}}
{{Cmd|grant all privileges on database mydb to myuser;}}
{{Cmd|grant all privileges on database database to user;}}
 
=== Network access ===


= Use =
By default only local access is allowed to PostgreSQL. To allow other networked services to access the database we need to configure PostgreSQL to allow external connections.
By default only local access is allowed to PostgreSQL. to allow other networked services to access the database we need to configure PostgreSQL to allow external connections.


== modify the postgresql.conf file ==
Edit the {{Path|/etc/postgresql16/postgresql.conf}} file using <code>nano</code> or any other {{ic|<editor> /etc/postgresql16/postgresql.conf}}
{{Cmd|nano /etc/postgresql16/postgresql.conf}}
Find the line that starts with <pre>#listen_addresses = 'localhost'</pre>
Find the line that starts with #listen_addresses = 'localhost' uncomment it and change localhost to *:
Uncomment it and change it to the following:
<pre>listen_addresses = '*'</pre>
<pre>listen_addresses = '*'</pre>
or change it to the IP address you want the server to listen on.
If you want it to listen on a specific ip you can change * to 192.168.1.2/24.
Save the file and change the next config file.
Save the file and change the next config file.


== modify the pg_hba.conf file ==
Modify the {{Path|/etc/postgresql16/pg_hba.conf}} file using <code>nano</code> or any other {{ic|<editor> /etc/postgresql16/pg_hba.conf}}
{{Cmd|nano /etc/postgresql16/pg_hba.conf}}
Look for the line: <pre>host    all            all            127.0.0.1/32            md5</pre>
look for the line: host    all            all            127.0.0.1/32            md5
And change it to: <pre>host all all 0.0.0.0/0 md5</pre>
and change it to: host all all 0.0.0.0/0 md5
This line allows connections from any IP address and requires a password for authentication (md5).
This line allows connections from any IP address and requires a password for authentication (md5).
Restart the server to allow incoming connections from other hosts. {{Cmd|rc-service postgresql restart}}
Allow the port through the firewall. For [[UFW]] firewall type: {{Cmd|ufw allow 5432}}


= Troubleshooting =
This is a basic configuration. You can configure the PostgreSQL server to only allow certain networks or IP's to connect but thats beyond the scope of this documentation.
* Some troubleshooting information


= Upgrading PostgreSQL =
== Upgrading PostgreSQL ==
* Notes on upgrading PostgreSQL 16 to 17


= Resources =
{{Todo| Need to add Notes on upgrading PostgreSQL }}
 
== Troubleshooting ==
 
{{Todo|Need to add troubleshooting examples}}
 
== See also ==
* https://blog.devart.com/configure-postgresql-to-allow-remote-connection.html
* https://blog.devart.com/configure-postgresql-to-allow-remote-connection.html


 
[[Category:Database]]


[[Category:Server]]
[[Category:Server]]

Latest revision as of 02:01, 18 February 2025

PostgreSQL is a well known opensource database that scales well and is easy to use. In Alpine v3.20 we can install the latest version using the package postgresql16

Installation

apk add postgresql16 postgresql16-contrib postgresql16-openrc

rc-update add postgresql

rc-service postgresql start

This will start the postgresql 16 server and perform some initial configuration.

Configuration

Login as the postgres user and start psql to create a new user and database:

su postgres

psql

create user user with encrypted password 'password';

create database database;

grant all privileges on database database to user;

Network access

By default only local access is allowed to PostgreSQL. To allow other networked services to access the database we need to configure PostgreSQL to allow external connections.

Edit the /etc/postgresql16/postgresql.conf file using nano or any other <editor> /etc/postgresql16/postgresql.conf

Find the line that starts with

#listen_addresses = 'localhost'

Uncomment it and change it to the following:

listen_addresses = '*'

If you want it to listen on a specific ip you can change * to 192.168.1.2/24. Save the file and change the next config file.

Modify the /etc/postgresql16/pg_hba.conf file using nano or any other <editor> /etc/postgresql16/pg_hba.conf

Look for the line:

host    all             all             127.0.0.1/32            md5

And change it to:

host all all 0.0.0.0/0 md5

This line allows connections from any IP address and requires a password for authentication (md5).

Restart the server to allow incoming connections from other hosts.

rc-service postgresql restart

Allow the port through the firewall. For UFW firewall type:

ufw allow 5432

This is a basic configuration. You can configure the PostgreSQL server to only allow certain networks or IP's to connect but thats beyond the scope of this documentation.

Upgrading PostgreSQL

Todo: Need to add Notes on upgrading PostgreSQL


Troubleshooting

Todo: Need to add troubleshooting examples


See also