在Linux环境下安装postgresql

进入到安装目录

[root@localhost postgresql-9.4.0]# pwd
/root/postgresql-9.4.0
[root@localhost postgresql-9.4.0]# ls -ltr
total 620
-rw-r--r--.  1 1107 1107   1209 Dec 15  2014 README
-rw-r--r--.  1 1107 1107   1489 Dec 15  2014 Makefile
-rw-r--r--.  1 1107 1107    283 Dec 15  2014 HISTORY
-rw-r--r--.  1 1107 1107   3620 Dec 15  2014 GNUmakefile.in
-rw-r--r--.  1 1107 1107   1192 Dec 15  2014 COPYRIGHT
-rw-r--r--.  1 1107 1107  68100 Dec 15  2014 configure.in
-rwxr-xr-x.  1 1107 1107 443397 Dec 15  2014 configure
-rw-r--r--.  1 1107 1107    385 Dec 15  2014 aclocal.m4
drwxrwxrwx. 59 1107 1107   4096 Dec 15  2014 contrib
drwxrwxrwx.  2 1107 1107   4096 Dec 15  2014 config
drwxrwxrwx.  3 1107 1107   4096 Dec 15  2014 doc
-rw-r--r--.  1 1107 1107  75392 Dec 15  2014 INSTALL
drwxrwxrwx. 15 1107 1107   4096 Dec 15  2014 src
[root@localhost postgresql-9.4.0]# 

配置postgresql安装目录

[root@localhost postgresql-9.4.0]# ./configure --prefix=/pgsql

进行源码编译

[root@localhost postgresql-9.4.0]# make

进行源码安装

[root@localhost postgresql-9.4.0]# make install

创建用户与组

[root@localhost postgresql-9.4.0]# groupadd postgres
[root@localhost postgresql-9.4.0]# useradd -g postgres postgres
[root@localhost postgresql-9.4.0]#  cd /pgsql/
[root@localhost pgsql]# chown postgres:postgres data/
chown: cannot access `data/': No such file or directory
[root@localhost pgsql]# ls -ltr
total 16
drwxr-xr-x. 6 root root 4096 Mar  9 06:57 include
drwxr-xr-x. 2 root root 4096 Mar  9 06:57 bin
drwxr-xr-x. 6 root root 4096 Mar  9 06:57 share
drwxr-xr-x. 4 root root 4096 Mar  9 06:57 lib
[root@localhost pgsql]# 

配置环境变量

[postgres@localhost ~]$ vi .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PGHOME=/pgsql
export PGHOME

export PGDATA=$PGHOME/data

PATH=$PATH:$HOME/bin:$PGHOME/bin

export PATH

使环境变量生效

[postgres@localhost ~]$ source .bash_profile

初始化postgresql数据库

[postgres@localhost bin]$ initdb
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /pgsql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
creating template1 database in /pgsql/data/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating collations ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    postgres -D /pgsql/data
or
    pg_ctl -D /pgsql/data -l logfile start

至此,完成postgresql数据库初始化

启动postgresql数据库实例

[postgres@localhost ~]$ pg_ctl start
server starting
[postgres@localhost ~]$ LOG:  database system was shut down at 2017-03-09 08:34:11 PST
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started

[postgres@localhost ~]$ 

查看postgresql数据库实例进程

[postgres@localhost ~]$ ps -ef | grep postgres
root      87655  67915  0 06:59 pts/1    00:00:00 su - postgres
postgres  87656  87655  0 06:59 pts/1    00:00:00 -bash
postgres  88006      1  0 08:39 pts/1    00:00:00 /pgsql/bin/postgres
postgres  88008  88006  0 08:39 ?        00:00:00 postgres: checkpointer process   
postgres  88009  88006  0 08:39 ?        00:00:00 postgres: writer process   
postgres  88010  88006  0 08:39 ?        00:00:00 postgres: wal writer process   
postgres  88011  88006  0 08:39 ?        00:00:00 postgres: autovacuum launcher process   
postgres  88012  88006  0 08:39 ?        00:00:00 postgres: stats collector process   
postgres  88033  87656 33 08:40 pts/1    00:00:00 ps -ef
postgres  88034  87656  0 08:40 pts/1    00:00:00 grep postgres
[postgres@localhost ~]$ 

连接postgresql数据库

[postgres@localhost ~]$ psql -h 127.0.0.1 -d postgres -U postgres
psql (9.4.0)
Type "help" for help.

postgres=# 
原文地址:https://www.cnblogs.com/RaymondBlog/p/6527743.html