注意我使用的shell 是BASH,如果你使用的其他的SHELL的话,注意语法的区别。
过程如下:
//进入到如下的目录
root# cd /usr/local
//建立一个新的目录
root# mkdir pgsql
//解压下载的文件
root# tar –zxvf postgresql-base-8.0.3.tar.gz
//进入解压后的目录
root# cd postgresql-base-8.0.3
运行脚本,在这里有可能出现一些问题。那么需要加一个选项:–without-readline
root# ./configure –prefix=/usr/local/pgsql
root# make
root# make install
接下来需要配置POSTGRESQL
//建立一个存储数据的目录
# mkdir /usr/local/pgsql/data
//建立用户
#sudo adduser -m POSTGRES
#sudo passwd POSTGRES
//使POSTGRES用户拥有该目录的权限
# chown postgres /usr/local/pgsql/data
//切换到POSTGRES用户
# su postgres
接下来需要设置环境变量
#sudo gedit /etc/environment
PGDATA = /usr/local/pgsql/data
PATH=variable to $PATH:/usr/local/pgsql/bin
其中上面的两个变量都需要在BASH.shrc中设置或者是在PROFILE中设置(具体的变量设置方法,请查看其他的文章)。
接下来需要运行在postgresql 安装目录bin下的一个脚本initdb。
该脚本将在/usr/local/pgsql/data 下产生一个postgresql.conf文件。
这个时候就可以说完成了POSTGRESQL 的安装。
官方安装文档:
On a Debian server:
# apt-get install postgresql-8.1 postgresql-client-8.1 postgresql-contrib-8.1
On an Ubuntu server:
# apt-get install postgresql-8.3 postgresql-client-8.3 postgresql-contrib-8.3
Ubuntu also has packages for Slony-I replication and the PostGIS spacial data extensions:
# apt-get install postgresql-8.3-slony1
# apt-get install postgresql-8.3-postgis
To manage a PostgreSQL service from an Ubuntu workstation, install these packages:
# apt-get install pgadmin3 postgresql-client-8.3 postgresql-doc-8.3
This installs the graphical management tool pgAdmin III , the command-line utilities, and the official documentation in HTML format.
Enter this URI in your Web browser to read the documentation:
file:///usr/share/doc/postgresql-doc-8.3/html/index.html
If you use Debian stable, install these packages:
# apt-get install pgadmin3 postgresql-client-8.1 postgresql-doc-8.1
First, set a strong password for the postgres role. This role automatically has unrestricted access to the cluster and everything held within those databases, so set a password for this role. To avoid potential risk, do this as soon as you have installed PostgreSQL, even if you do not currently intend to enable remote access, and are sure that no other users can login on the local system.
As the configuration defaults to ident authentication for local access, we must use the system account postgres to login with the postgres role. Enter this command at the server:
sudo postgres psql
Once logged in to the SQL interface, set a password for the postgres role:
ALTER ROLE postgres WITH ENCRYPTED PASSWORD 'mypassword';
You need this password to connect to the PostgreSQL server remotely with the postgres role, as described below.
To exit the PostgreSQL shell, type:
/q
To enable some of the functions of the pgAdmin utility, you must run a script against the postgres database:
sudo -u postgres psql -d postgres < /usr/share/postgresql/8.1/contrib/admin81.sql
Use /usr/share/postgresql/8.3/contrib/adminpack.sql on current versions of Ubuntu.
If you wish to enable remote access, modify the cluster settings. The configuration files for each cluster are in the directory /etc/postgresql/version/clustername/, e.g. /etc/postgresql/8.1/main/.
Edit the file postgresql.conf, and remove the comment marker on the line for the listen_addresses setting, so that it reads:
listen_addresses = '*'
Open the file pg_hba.conf:
# nano /etc/postgresql/8.1/main/pg_hba.conf
To permit users to connect from remote systems on your network with any role, add this line:
host all all 192.168.1.0/24 md5
Replace 192.168.1.0/24 with the appropriate subnet definition for your network.
This enables md5 authentication, which means that login roles are secured with passwords that PostgreSQL itself stores in an encrypted form, and that PostgreSQL will require a valid password for any remote connection to use a role. After you make this change, ident authentication remains enabled for local logins.
You may, of course, change the local line in pg_hba.conf to disable ident authentication. Make sure that you can actually login to your PostgreSQL cluster with the postgres role and a password first!
To make your changes take effect, restart the service:
# /etc/init.d/postgresql-8.1 restart
Once the service restarts, you may access your PostgreSQL service from remote systems, either using tools such as psql or pgAdmin III , or with applications that support SQL . The Debian version of PostgreSQL is configured to automatically protect network connections with SSL , so that all communication between the server and remote clients is encrypted.
By default, the postgres system account on Debian is locked, and you should not unlock it. Cracking tools now try to use postgres, root, and other well-known system account names when they attempt to gain access to UNIX -like operating systems.
Under no circumstance should you enable PostgreSQL ident authentication for any remote access. The ident system cannot safely verify or guarantee the identity of any user on a remote system.
You may either use the createdb command-line utility to create new databases, or enter the appropriate SQL statement.
Remember that PostgreSQL does not create databases with UTF -8 by default. If you require Unicode support you must explicitly specify the UTF -8 encoding when you create the database:
# -u postgres createdb my_database -E UTF-8 --password
The corresponding SQL statement is:
CREATE DATABASE 'my_database' WITH ENCODING 'UTF-8';
You must manually set up an appropriate backup routine (see below).
The standard configuration enables the PostgreSQL auto-vacuum service by default, to ensure that the health of your databases is maintained. This typically just works without any intervention or extra configuration being required. If very large volumes of data are frequently added to or removed from your databases you may adjust the auto-vacuum settings to be more aggressive, in order to keep your clusters well optimized.
The Debian packages also install a scheduled task to automatically update the database statistics and run a standard vacuum each day if the auto-vacuum service is not running on the system (for whatever reason). In this case, edit /etc/cron.d/postgresql-common/ to enable a weekly full VACUUM . These tasks use the utility /usr/sbin/pg_maintenance, which you may also run manually. Refer to the man page for pg_maintenance for more information.
The correct backup strategy for your databases depends on their size and rate of change. For smaller databases, the best solution is to simply use the supplied pg_dumpall utility. Refer to the documentation for a detailed explanation of backup options:
http://www.postgresql.org/docs/8.3/static/backup.html
Debian includes additional facilities to support multiple PostgreSQL clusters on the same system. Refer to the file /usr/share/doc/postgresql-common/architecture.html for a brief explanation, and the relevant man pages for more details:
Clusters may use different versions of PostgreSQL. Install the packages for the appropriate PostgreSQL versions, and specify the version that you require when you make a new cluster with pg_createcluster.
To safely upgrade a PostgreSQL cluster, use the pg_upgradecluster utility. This command actually creates a new cluster with the data and configuration from the specified cluster. The utility also automatically reconfigures the existing database cluster to use a different network port. The original copy of the specified cluster is not altered in any other way.
All original content is © 2010, Stuart Ellis.
This material is provided under the Creative Commons Attribution-Share Alike 3.0 License .