|
PostgreSQL Database Server Configuration in Debian Introduction
PostgreSQL is a fully featured object-relational database management system. It supports a large part of the SQL standard and is designed to be extensible by users in many aspects. Some of the features are: ACID transactions, foreign keys, views, sequences, subqueries, triggers, user-defined types and functions, outer joins, multiversion concurrency control. Graphical user interfaces and bindings for many programming languages are available as well.
Postgresql Database Installation in Debian If you want to install the PostgreSQL base system as well as a textual clientrun the following command from your shell #apt-get install postgresql postgresql-client This will install a lot of package dependencies as well. The version of PostgreSQL installed should be 7.4 on a common Debian Sarge system. If you want to check the official documentation click here. A few questions will be asked by the configuration script. Most of them can be simply agreed, but you might want to read them to know what configuration PostgreSQL will use. Data removal policy Also by default, a PostgreSQL system removal won't remove the data (database contents). Date Choose European or US as your preferred date format. The install continues with several default parameters, and then creates /etc/postgresql/postgresql.conf, which will hold your configuration settings. Creating Users for your Database The only (default) way to log into your newly created database system is to log as root, then to log as user postgres, by issuing #su However, you would probably like to be able to login as your unix user, or any other user you choose. There are several ways to set up these accesses, so let's decomposeAlowing local users to login is done by editing the /etc/postgresql/pg_hba.conf file (a complete documentation of this file is available here). There, you have to retrieve a series of lines like the following ones local all postgres ident sameuser This file has limited accesses but we will come back here later.Now what we have told PostgreSQL is that all users on the local machine should be able to access all databases, using their UNIX login and passwords. You need to restart your database to take new changes effect Restart postgresql using the following command #/etc/init.d/postgresql restart Now the problem is that you have authorized users to log into the database, but they will only be able to log in if they also exist in the database. So you need to create accounts with their usernames. This is done by issuing the createuser command as user postgres as follows #su postgres The createuser command will ask you if the user should be able to create databases or create new users. Answer truly. Now you have a new user in the database, and you should be able to connect as this user (let's call him firstuser) like this: #su - firstuser The -W parameter is to ask for a password prompt. You should enter your system password when asked. If some of your web scripts are going to need a connection to the database, you will need to grant them some access via a new user, reserved for this web usage. Let's first create this new user (as user postgres). Let's call him webuser. #su postgres Now you want to give him access to one or several databases (let's say you will let him access the databases which name is web). To do this, you need to edit once more the pg_hba.conf file. Retrieve the first line below and write the second one just beneath the first. host all all 127.0.0.1 255.255.255.255 md5 Would you like to give this user access from any computer on the sub-network 192.168.0.xxx, you would have to add the following line host web webuser 192.168.0.1 255.255.255.0 md5 You have to grant him access to the host as it will probably use the TCP/IP protocol to get a connection to the database, and that's what host is for. But as you have given him an md5 authentication type, you need to give him a password as well. In order to do this, you need to connect to the database and issue a special command, all as postgres user: #su postgres Now the user should have a nice 'some_password' password that you can use after restarting PostgreSQL to make it remember your changes to pg_hba.conf. Restart postgresql using the following command #/etc/init.d/postgresql restart And you should be able to create his own database if you gave him the permission #createdb -h localhost -U webuser -W web And connect to this newly-created database using the following command #psql -h localhost -U webuser -W web As you might have noticed, we use -h localhost here. This is to force the PostgreSQL client to use TCP/IP instead of UNIX sockets. If you don't want is, you need to add a new line into the "local" category of pg_hba.conf for the user webuser to connect via sockets.The line would be something like local web webuser md5 Using SQL The first important command you might want to know is #psql -h localhost -U webuser -W -l This will give you a list of available databases. Now connect to the PostgreSQL database using the psql client, and let's create a small table, just after a few tests web=> SELECT version(); Now you've had a nice overview of several SQL statements and how to use them in PostgreSQL. Later, you might want to create a table with a numerical sequence as an index. This link will help you get through this step which I find counter-intuitive and user-unfriendly at will. You might as well know of a shortcut. If you want to create a default auto-incremental key that will be used by your table, you need to define (for every table) a SEQUENCE. This id done, for the weather table above, like this (with the help of user comments here): web=> CREATE SEQUENCE my_table_pk_sequence; And then insert elements as before, ignoring the id field web=> INSERT INTO weather (date, city, temp_hi, temp_lo) This should have inserted an auto-incremented id. If you want to install ODBC Support follow this First install ODBC support packages in debian #apt-get install odbc-postgresql This should need about 1 MB on your system's hard drive.This should be enough to form a DSN name in order to connect to your table. A good source of information on the topic can be found here. Using the parameters we used here above, you should be able to connect with something like: Server=localhost{ or the IP address of your PostgreSQL server};Database=web;Uid=webuser;Pwd=some_password" Note that we use MD5 encryption, so you might need to specify that somewhere (and encrypt your password before you put it in the connection string). If you don't want to do this through command line no problem you can use web interface tools for postgresql.If you want to use this click here |