This article will guide you through the Gentoo specific steps to install the PostgreSQL RDBMS.
The Ebuilds covered by this article are
This article assumes that you will be installing the latest, stable version of PostgreSQL; at the time of writing, the version was 8.4.4. Adjust the commands in this article as necessary for your specific version.
The PostgreSQL ebuilds in Portage feature slotting based on the major version. This allows you to have two major versions of PostgreSQL operating simultaneously. For instance, 8.3 and 8.4 can be installed at the same time. This is useful in such circumstances where you need to move data from an older database to a new database, or need to have a production and a testing database on the same machine. Also, this prevents a database, corresponding libraries or executables from being overwritten by an incompatible update.
Additionally, bug and security fixes, which are delivered via minor version updates, can be applied without fear of corrupting data; 8.4.2 can be updated to 8.4.4 as they are guaranteed to be compatible and require no more interaction from you than to emerge it and restart the server process: no migration, reconfiguration or initialization necessary.
Read the
There is quite a bit that will not be covered. The
If you have any of the following ebuilds installed, then you have an older, obsolete Gentoo installation of PostgreSQL: dev-db/postgresql-libs, dev-db/postgresql-client, dev-db/libpq and/or dev-db/postgresql.
This article does cover
USE Flag | Meaning |
---|---|
# emerge -av dev-db/postgresql-server [ebuild N ] dev-db/postgresql-docs-8.4.4 0 kB [ebuild N ]dev-db/postgresql-base-8.4.4-r2 USE="doc nls pam readline ssl zlib -kerberos -ldap -pg_legacytimestamp -threads" LINGUAS="-af -cs -de -es -fa -fr -hr -hu -it -ko -nb -pl -pt_BR -ro -ru -sk -sl -sv -tr -zh_CN -zh_TW" 0 kB [ebuild N ] dev-db/postgresql-server-8.4.4-r1 USE="doc nls perl python -pg_legacytimestamp (-selinux) -tcl -uuid -xml" LINGUAS="-af -cs -de -es -fa -fr -hr -hu -it -ko -nb -pl -pt_BR -ro -ru -sk -sl -sv -tr -zh_CN -zh_TW" 0 kB
You may receive a notice regarding that any of the above packages are blocked by
any or all of the following packages: dev-db/postgresql-libs,
dev-db/postgresql-client, dev-db/libpq or dev-db/postgresql. These packages are
not maintained and obsoleted. Refer to the section on
Once the packages have finished emerging, you may want to edit
In the following example,
# PostgreSQL's Database Directory PGDATA="/var/lib/postgresql/8.4/data" # Options to pass to PostgreSQL's initdb. PG_INITDB_OPTS="--locale=en_US.UTF-8"
This only determines the default locale and character encoding. You can specify different locales and/or character encodings at the database or table level in the same database cluster.
There are six locale options that can be set to override
Option | Effects |
---|---|
So, if you would like the default to be English, but you want messages in, say,
Swedish, then your
PG_INITDB_OPTS="--locale=en_US.UTF-8 --lc-messages=sv_SE.UTF-8"
A complete list of language and character encodings supported by the server can
be found in the documentation, but your system must also support the respective
languages and character encodings. Compare the output of
You can change your locale and encoding selections at database creation time
(
To finish the installation:
# emerge --config =dev-db/postgresql-server-8.4.4-r1
This will create the database cluster and store all the related server files
into
This time the focus is upon the files in the
This is the main configuration file. The line that you may find of immediate
interest is
Of secondary interest is the logging destination. By default, everything is
logged to
Other than
The
# TYPE DATABASE USER CIDR-ADDRESS METHOD # "local" is for Unix domain socket connections only local all all trust# IPv4 local connections: host all all 127.0.0.1/32 trust# IPv6 local connections: host all all ::1/128 trust
As has been mentioned before, by default the server is secure. Kind of. There is
only one database role that is available for log in by default:
To make a connection through the Unix socket, however, the users -
including the users for other services such as
The
The two methods you will most likely use are:
At this point, we would like to bring your attention to the last two lines (or four lines including comments) of the configuration file. PostgreSQL has native support for IPv6 regardless of your desires for such support. Additionally, IPv4 addresses are automatically mapped to IPv6 addresses, so 127.0.0.1 will be mapped to ::FFFF:127.0.0.1 and as "pure" IPv6 ::FFFF:7F00:0001.
There seems to be some misunderstanding, though, as to how host names are mapped
to IP addresses. Let us take a look at the
# IPv4 and IPv6 localhost aliases 127.0.0.1 localhost ::1 localhost
From the example above you can see that both an IPv4 and an IPv6 IP address are
mapped to localhost. When
So, it is better to specify IP addresses alone to
Now start PostgreSQL and set the password for the database superuser
(Change 'trust' to 'password' for the localhost connections.) # nano -w /var/lib/postgresql/8.4/data/pg_hba.conf # /etc/init.d/postgresql-8.4 start postgresql-8.4 | * Starting PostgreSQL ... [ ok ](Open a connection to the server and set the password.) # psql -U postgres psql (8.4.4) Type "help" for help. postgres=# \password Enter new password: Enter it again: postgres=# \q(Change 'trust' to 'password' for the local connection.) # nano -w /var/lib/postgresql/8.4/data/pg_hba.conf # /etc/init.d/postgresql-8.4 reload postgresql-8.4 | * Reloading PostgreSQL configuration ... [ ok ] # rc-update add postgresql-8.4 default * service postgresql-8.4 added to runlevel default
You only need to perform a migration when moving from one major version to another, for instance from PostgreSQL 8.3.11 to 8.4.4, but not from 8.4.2 to 8.4.4.
Because the new ebuilds feature a more advanced slotting method than the previous ones, the downtime is quite minimal, most likely minutes rather than hours.
In the following examples, it is assumed that you are using the default locations and port settings, and that you are migrating from 8.3 to 8.4. Adjust accordingly if you have deviated from the default.
If you have not already done so, follow the
A couple of files need to be tweaked before beginning the migration. Edit
Next, edit
# cd /var/lib/postgresql/ # cp -p 8.3/data/pg_hba.conf 8.4/data/(The following should be safe. Read the documentation to be sure.) # cp -p 8.3/data/postgresql.conf 8.4/data/(Don't forget to copy over any other configuration files that you may need.) # /etc/init.d/postgresql-8.3 reload # /etc/init.d/postgresql-8.4 start(Begin piping the data from the old cluster to the new cluster.) # pg_dumpall -U postgres -p 5432 | psql -U postgres -d postgres -p 6543 # /etc/init.d/postgresql-8.3 stop # /etc/init.d/postgresql-8.4 stop(Edit PGPORT back to 5432.) # nano -w /etc/conf.d/postgresql-8.4(Allow users access once more.) # nano -w /var/lib/postgresql/8.4/data/pg_hba.conf # /etc/init.d/postgresql-8.4 start # rc-update del postgresql-8.3 && rc-update add postgresql-8.4 default
Hopefully everything went according to plan and you have a successfully updated server that contains precisely the same data, bit for bit, as the old server.
You will need to schedule some downtime for your server. The old ebuilds cannot be installed at the same time as the new Ebuilds. As such, assume that the server will have to be down for a few hours. Maybe for the weekend, even.
Before starting, you will need to deny access to the server, so that no changes
are made. You may also want to backup your
# pg_dumpall -U postgres > backup_file # /etc/init.d/postgresql stop # emerge -C dev-db/postgresql dev-db/libpq dev-db/postgresql-client \ dev-db/postgresql-client(Follow the steps detailed in this article for installing and configuring the server.) # /etc/init.d/postgresql-8.4 start # psql -f backup_file postgres
You may break some packages that were built against those ancient packages, but
once you have installed dev-db/postgresql-base and/or dev-db/postgresql-server
you can run
This problem is easy to solve. What is difficult about it is finding the
answer. What is required is an import from a file that already exists on the
storage drive:
# psql -U postgres --file /usr/share/postgresql-8.4/contrib/adminpack.sql
If you get an error upon emerging dev-db/postgresql-base that reads as follows:
# emerge dev-db/postgresql-base . . . configure: error: thread test program failed This platform is not thread-safe. Check the file 'config.log' for the exact reason. You can use the configure option --enable-thread-safety-force to force threads to be enabled. But you must then run the program in src/test/thread and add locking function calls to your applications to guarantee thread safety.
The solution, in most cases, is to update sandbox to a version greater than or equal to 2.0.
# echo '>=sys-apps/sandbox-2.0' >> /etc/portage/package.keywords # emerge -av '>=sys-apps/sandbox-2.0' # emerge -av dev-db/postgresql-base