二进制安装postgres比较简单,网上例子很多,本文介绍二进制安装postgres方式。
下载二进制postgres文件
[root@qxy spark]# ls -ltr postgresql-9.6.4-1-linux-x64-binaries.tar.gz -rw-r--r--. 1 postgres postgres 150077822 May 9 11:11 postgresql-9.6.4-1-linux-x64-binaries.tar.gz [root@qxy spark]#
1、创建postgres用户和组
[root@qxy ~]# groupadd postgres [root@qxy ~]# useradd -g postgres postgres2、创建postgres安装目录
[root@qxy ~]# mkdir -p /spark/pgsql/data [root@qxy ~]# chown -R postgres:postgres /spark/pgsql/
3、解压postgresql-9.6.4-1-linux-x64-binaries.tar.gz
postgres@qxy spark]$ tar -zxvf postgresql-9.6.4-1-linux-x64-binaries.tar.gz -C /spark/pgsql/ [postgres@qxy pgsql]$ pwd /spark/pgsql [postgres@qxy pgsql]$ ls -ltr total 12 drwxrwxr-x. 9 postgres postgres 4096 Aug 8 2017 pgsql <=====解压得到的目录 drwxrwxr-x. 2 postgres postgres 4096 Jun 6 08:31 data [postgres@qxy pgsql]$ [postgres@qxy pgsql]$ pwd /spark/pgsql/pgsql [postgres@qxy pgsql]$ ls -ltr total 28 drwxrwxr-x. 4 postgres postgres 4096 Aug 8 2017 share drwxrwxr-x. 11 postgres postgres 4096 Aug 8 2017 include drwxrwxr-x. 9 postgres postgres 4096 Aug 8 2017 pgAdmin 4 drwxrwxr-x. 5 postgres postgres 4096 Aug 8 2017 stackbuilder drwxrwxr-x. 2 postgres postgres 4096 Aug 8 2017 bin drwxrwxr-x. 4 postgres postgres 4096 Aug 8 2017 lib drwxrwxr-x. 3 postgres postgres 4096 Aug 8 2017 doc [postgres@qxy pgsql]$
4、初始化数据库
[postgres@qxy pgsql]$ /spark/pgsql/pgsql/bin/initdb -E utf8 -D /spark/pgsql/data/ The files belonging to this database system will be owned by user "postgres". This user must also own the server process. initdb: invalid locale settings; check LANG and LC_* environment variables [postgres@qxy pgsql]$ [postgres@qxy pgsql]$ export LC_ALL=C <========设置LC_ALL环境变量 [postgres@qxy pgsql]$ /spark/pgsql/pgsql/bin/initdb -E utf8 -D /spark/pgsql/data/ 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 "C". The default text search configuration will be set to "english". Data page checksums are disabled. fixing permissions on existing directory /spark/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 running bootstrap script ... ok performing post-bootstrap initialization ... locale: Cannot set LC_MESSAGES to default locale: No such file or directory No usable system locales were found. Use the option "--debug" to see details. 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: /spark/pgsql/pgsql/bin/pg_ctl -D /spark/pgsql/data/ -l logfile start [postgres@qxy pgsql]$
5、配置profile中的环境变量
[postgres@qxy ~]$ cat .bash_profile # .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME/bin export PGHOME=/spark/pgsql/pgsql export PGDATA=/spark/pgsql/data PATH=$PATH:$HOME/bin:$PGHOME/bin export LD_LIBRARY_PATH=/opt/glibc-2.14/lib:$LD_LIBRARY_PATH export PATH [postgres@qxy ~]$
6、启动postgres数据库
[postgres@qxy ~]$ pg_ctl start -D $PGDATA server starting [postgres@qxy ~]$ LOG: database system was shut down at 2018-06-06 08:37:22 GMT LOG: MultiXact member wraparound protections are now enabled LOG: database system is ready to accept connections LOG: autovacuum launcher started [postgres@qxy ~]$ ps -ef | grep postgres root 16339 12615 0 08:27 pts/4 00:00:00 su - postgres postgres 16340 16339 0 08:27 pts/4 00:00:00 -bash postgres 16521 1 0 08:41 pts/4 00:00:00 /spark/pgsql/pgsql/bin/postgres -D /spark/pgsql/data postgres 16523 16521 0 08:41 ? 00:00:00 postgres: checkpointer process postgres 16524 16521 0 08:41 ? 00:00:00 postgres: writer process postgres 16525 16521 0 08:41 ? 00:00:00 postgres: wal writer process postgres 16526 16521 0 08:41 ? 00:00:00 postgres: autovacuum launcher process postgres 16527 16521 0 08:41 ? 00:00:00 postgres: stats collector process postgres 16537 16340 1 08:42 pts/4 00:00:00 ps -ef postgres 16538 16340 0 08:42 pts/4 00:00:00 grep postgres [postgres@qxy ~]$
7、登陆数据库
[postgres@qxy ~]$ psql psql.bin (9.6.4) Type "help" for help. postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+---------+-------+----------------------- postgres | postgres | UTF8 | C | C | template0 | postgres | UTF8 | C | C | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | C | C | =c/postgres + | | | | | postgres=CTc/postgres (3 rows) postgres=#
8、关闭postgres数据库
[postgres@qxy ~]$ pg_ctl stop -D $PGDATA -m fast waiting for server to shut down....LOG: received fast shutdown request LOG: aborting any active transactions LOG: autovacuum launcher shutting down LOG: shutting down LOG: database system is shut down done server stopped
9、pg_ctl说明
[postgres@qxy ~]$ pg_ctl --help pg_ctl is a utility to initialize, start, stop, or control a PostgreSQL server. Usage: pg_ctl init[db] [-D DATADIR] [-s] [-o "OPTIONS"] pg_ctl start [-w] [-t SECS] [-D DATADIR] [-s] [-l FILENAME] [-o "OPTIONS"] pg_ctl stop [-W] [-t SECS] [-D DATADIR] [-s] [-m SHUTDOWN-MODE] pg_ctl restart [-w] [-t SECS] [-D DATADIR] [-s] [-m SHUTDOWN-MODE] [-o "OPTIONS"] pg_ctl reload [-D DATADIR] [-s] pg_ctl status [-D DATADIR] pg_ctl promote [-D DATADIR] [-s] pg_ctl kill SIGNALNAME PID Common options: -D, --pgdata=DATADIR location of the database storage area -s, --silent only print errors, no informational messages -t, --timeout=SECS seconds to wait when using -w option -V, --version output version information, then exit -w wait until operation completes -W do not wait until operation completes -?, --help show this help, then exit (The default is to wait for shutdown, but not for start or restart.) If the -D option is omitted, the environment variable PGDATA is used. Options for start or restart: -c, --core-files allow postgres to produce core files -l, --log=FILENAME write (or append) server log to FILENAME -o OPTIONS command line options to pass to postgres (PostgreSQL server executable) or initdb -p PATH-TO-POSTGRES normally not necessary Options for stop or restart: -m, --mode=MODE MODE can be "smart", "fast", or "immediate" Shutdown modes are: smart quit after all clients have disconnected fast quit directly, with proper shutdown immediate quit without complete shutdown; will lead to recovery on restart Allowed signal names for kill: ABRT HUP INT QUIT TERM USR1 USR2 Report bugs to <[email protected]>. [postgres@qxy ~]$