Postgresql installation and configuration
url: "/Installation_et_configuration_de_PostgreSQL/" title: "PostgreSQL Installation and Configuration" description: "A comprehensive guide to installing, configuring, and managing PostgreSQL databases including user management, backups, and basic SQL operations." categories: ["PostgreSQL", "Database"] date: "2012-11-16T08:55:00+02:00" lastmod: "2012-11-16T08:55:00+02:00" tags: ["PostgreSQL", "Database", "SQL", "Backup", "User Management"]
Software version | 8.3+ |
Operating System | Debian 6 |
Website | PostgreSQL Website |
Introduction
PostgreSQL is an object-relational database management system (ORDBMS). It is a free tool available under a BSD-like license.
This system competes with other database management systems, both free (like MySQL and Firebird) and proprietary (like Oracle, Sybase, DB2, and Microsoft SQL Server). Like the Apache and Linux projects, PostgreSQL is not controlled by a single company but is based on a global community of developers and companies.
Installation
To install PostgreSQL:
This creates the postgres user who has rights over the database. To initialize a PostgreSQL database in the postgres user's $HOME (/var/lib/postgres on Debian) (the installation on Debian does this automatically):
Note: You need to have correct permissions on /tmp/
On Debian 5 (lenny), to initialize the main postgres database, run the following command as root:
Configuration files are located in $HOME/data, particularly pg_hba.conf for access rights management and postgresql.conf for general service configuration. On Debian, these files are symbolic links to files in /etc/postgresql/.
Configuration
User Management
Admin
First, let's change the password:
This will modify the postgres password on the machine, but not on the database. You don't have to do this if you don't need to (e.g., if you always go through root then postgres). Now, let's define a password for the database:
or
This will make the user admin of the template1 database.
Note: To use web clients or graphical clients presented in the following chapters, you must define a password for "postgres".
Authentication
By default, to access PostgreSQL, you need to connect as the "postgres" user. To create a new user, you would need to create a system account for them first, which may not be desirable. To change this configuration, modify the "/etc/postgresql/pg_hba.conf" file and replace "ident sameuser" with "password" on the following two lines:
This becomes:
This modification avoids using system accounts and requires a password for each connection.
To allow another machine to connect to this Postgres server, add a line like this to the "/etc/postgresql/pg_hba.conf" file:
Warning
WARNING: The order of insertion of the lines is very important! The first line that matches will be the one that takes the rule. So be careful about the insertion order in this file.
And modify the "/etc/postgresql/postgresql.conf" file by adding the following option so that postgres listens on all its addresses and not just "localhost":
Restart PostgreSQL:
Creation
To create a user:
By default, this user has no password. To assign one:
Suppression
To delete a user:
Database Management
Creation
The following command creates the "mybase" database for the user "toto" using the "UNICODE" encoding:
Be careful with the table encoding: LATIN9, LATIN1, UNICODE, etc.
Suppression
To delete a database:
Listing
To list existing databases:
Backing Up a Database
To back up a database:
Backing Up All Databases
To back up all databases at once:
Backups on Very Large Databases
It seems that by passing certain parameters, it is easier to restore databases:
https://www.postgresql.org/docs/8.1/static/app-pgdump.html Note: Be aware of some limitations in "hot" backups.
Restoration
To restore a database, first create an empty database, then import:
Displaying All Queries
You can increase the log level to see all queries that pass through. These queries will be recorded in a file.
Warning
This can slow down your system if you have a lot of activity
To set up a higher log level, edit these lines in the PostgreSQL configuration:
Just restart your PostgreSQL to see logs appear in /var/lib/postgresql/
Usage
You can now "use" your database with the PostgreSQL command line client:
Here are some useful commands to remember:
Creating and Deleting Tables
Here are the different data types for table fields:
Note: You can also define your own data types.
The creation syntax:
Deletion:
As a small example taken from the PostgreSQL documentation:
Info
Two dashes -- introduce comments...
Data Extraction
Nothing beats examples:
With joins:
With functions (Aggregate Functions):
Note that "Aggregate Functions" cannot be used in the WHERE clause. So the following query is incorrect:
You should do instead:
You can, of course, use "GROUP BY ...", "HAVING ...", etc.
Data Updates
Still with an example:
Data Deletion
Again with an example:
To delete all data from a table:
Querying a Database Size
FAQ
Tsearch
If an application requires tsearch2 for example, you need to install a package:
Use your corresponding version number at the end of the package.
And finally, you need to patch the database in question:
Here wikidb is my database, and 8.2 is the postgres version again.
UTF8 Encoding Doesn't Match the Locale
The annoying error that bugged me. This happens when you want to create a database that's not in the server's current encoding:
To fix the problem, first check that the locales are good at the OS level:
Then, let's say I want UTF-8, I set my environment variables properly:
Now I can list the postgres environment variable:
Normally if you create a new database, the encoding will now be UTF-8. If that still doesn't change anything, you must run this, but it will put all your future databases in this format:
Now try creating a database again.