Postgresql: Difference between revisions

From Alpine Linux
m (Better generalize certain sections)
(simplified repetetive information)
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
PostgreSQL is a well known opensource database that scales well and is easy to use.
PostgreSQL is a well known opensource database that scales well and is easy to use.


== PostgreSQL 17 ==
== Installation ==
 
PostgreSQL 17 is the latest version since [[Release_Notes_for_Alpine_3.21.0#PostgreSQL_17|v3.21]].


To install PostgreSQL 17,
To install PostgreSQL 17,
{{Cmd|apk add postgresql17 postgresql17-contrib postgresql17-openrc}}
{{Cmd|# apk add postgresql17 postgresql17-contrib postgresql17-openrc}}
{{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 server and perform some initial configuration. It will also create a system user called postgres.
 
To install, configure, and use PostgreSQL 17, which is the latest release as of Alpine v3.21, please refer to the [https://wiki.alpinelinux.org/wiki/Postgresql_16 PostgreSQL 16 Alpine Wiki page]. It will also create a system user called postgres.
 
== PostgreSQL 16 ==


To install PostgreSQL 16,
This will start the postgresql server and perform some initial configuration. It will also create a system user called '''postgres'''.
{{Cmd|apk add postgresql16 postgresql16-contrib postgresql16-openrc}}
{{Cmd|rc-update add postgresql}}
{{Cmd|rc-service postgresql start}}


This will start the postgresql server and perform some initial configuration. It will also create a system user called postgres.
Older versions of PostgreSQL 16, PostgreSQL 15 and PostgreSQL 14 are still available as of [[Release Notes for Alpine 3.20.0|v3.22]] and can be installed using the same commands as above except for replacing the version number in the appropriate package names. For example, {{pkg|postgresql17}} is to be replaced by {{pkg|postgresql16}}, {{pkg|postgresql15}} and {{pkg|postgresql14}} for PostgreSQL 16, PostgreSQL 15 and PostgreSQL 14 respectively.


To install, configure, and use PostgreSQL 16, please refer to the [https://wiki.alpinelinux.org/wiki/Postgresql_16 PostgreSQL 16 Alpine Wiki page]. It will also create a system user called postgres.
== Configuration ==


== PostgreSQL 15 ==
Login as the postgres user and start psql to create a new user and database:
{{Cmd|su postgres}}
{{Cmd|psql}}
{{Cmd|create user user with encrypted password 'password';}}
{{Cmd|create database database;}}
{{Cmd|grant all privileges on database database to user;}}


To install PostgreSQL 15,
=== Network access ===
{{Cmd|apk add postgresql15 postgresql15-contrib postgresql15-openrc}}
{{Cmd|rc-update add postgresql}}
{{Cmd|rc-service postgresql start}}


This will start the postgresql server and perform some initial configuration. It will also create a system user called postgres.
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.


To configure and use PostgreSQL, please refer to the [https://wiki.alpinelinux.org/wiki/Postgresql_16 PostgreSQL 16 Alpine Wiki page] as the usage is the same.
In this example, we are using PostgreSQL v16, so substitute below for your specific version.
Edit the {{Path|/etc/postgresql16/postgresql.conf}} file using <code>nano</code> or any other {{ic|<editor> /etc/postgresql16/postgresql.conf}}
Find the line that starts with <pre>#listen_addresses = 'localhost'</pre>
Uncomment it and change it to the following:
<pre>listen_addresses = '*'</pre>
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.


== PostgreSQL 14 ==
Modify the {{Path|/etc/postgresql16/pg_hba.conf}} file using <code>nano</code> or any other {{ic|<editor> /etc/postgresql16/pg_hba.conf}}
Look for the line: <pre>host    all            all            127.0.0.1/32            md5</pre>
And change it to: <pre>host all all 0.0.0.0/0 md5</pre>
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}}


To install PostgreSQL 14,
Allow the port through the firewall. For [[UFW]] firewall type: {{Cmd|ufw allow 5432}}
{{Cmd|apk add postgresql14 postgresql14-contrib postgresql14-openrc}}
{{Cmd|rc-update add postgresql}}
{{Cmd|rc-service postgresql start}}


This will start the postgresql server and perform some initial configuration. It will also create a system user called postgres.
This is a basic configuration. You can configure the PostgreSQL server to only allow certain networks or IP's to connect, but that's beyond the scope of this documentation.
 
To configure and use PostgreSQL, please refer to the [https://wiki.alpinelinux.org/wiki/Postgresql_16 PostgreSQL 16 Alpine Wiki page] as the usage is the same.


== Switching between PostgreSQL versions ==
== Switching between PostgreSQL versions ==
Line 53: Line 53:
Please follow along with [https://perrotta.dev/2025/05/postgresql-major-version-upgrade-on-alpine-linux/ this guide] and adjust the version you are moving from and the version you are upgrading to.
Please follow along with [https://perrotta.dev/2025/05/postgresql-major-version-upgrade-on-alpine-linux/ this guide] and adjust the version you are moving from and the version you are upgrading to.


There's also [https://beune.dev/posts/upgrade-alpine-postgresql/ this older guide].
There's also [https://beune.dev/posts/upgrade-alpine-postgresql/ this].
 
== See also ==
* [https://blog.devart.com/configure-postgresql-to-allow-remote-connection.html Guide for Upgrading PostgreSQL]
* [https://beune.dev/posts/upgrade-alpine-postgresql/  older postgresql upgrade guide]


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

Latest revision as of 16:55, 31 October 2025

PostgreSQL is a well known opensource database that scales well and is easy to use.

Installation

PostgreSQL 17 is the latest version since v3.21.

To install PostgreSQL 17,

# apk add postgresql17 postgresql17-contrib postgresql17-openrc

# rc-update add postgresql

# rc-service postgresql start

This will start the postgresql server and perform some initial configuration. It will also create a system user called postgres.

Older versions of PostgreSQL 16, PostgreSQL 15 and PostgreSQL 14 are still available as of v3.22 and can be installed using the same commands as above except for replacing the version number in the appropriate package names. For example, postgresql17 is to be replaced by postgresql16, postgresql15 and postgresql14 for PostgreSQL 16, PostgreSQL 15 and PostgreSQL 14 respectively.

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.

In this example, we are using PostgreSQL v16, so substitute below for your specific version. 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 that's beyond the scope of this documentation.

Switching between PostgreSQL versions

On Alpine Linux, you can use the command `pg_versions` to switch between PostgreSQL versions. This is very helpful when upgrading the version you are using.

Upgrading PostgreSQL

Please follow along with this guide and adjust the version you are moving from and the version you are upgrading to.

There's also this.

See also