Centos7二进制安装Postgresql-10.15

1.创建postgre所需用户

groupadd pgsql
useradd -g pgsql pgsql
passwd pgsql

2.下载postgre二进制安装包

下载地址:https://www.enterprisedb.com/download-postgresql-binaries
wget -P /usr/local/src https://get.enterprisedb.com/postgresql/postgresql-10.15-1-linux-x64-binaries.tar.gz

3.解压tar包并移动至相关目录,并创建data、log目录

cd /usr/local/src && tar xf postgresql-10.15-1-linux-x64-binaries.tar.gz
mv pgsql ../
mkdir -p /usr/local/pgsql/{data,log}

4.给data,log目录授权

chown -R pgsql.pgsql /usr/local/pgsql 
chmod -R 755 /usr/local/pgsql

5.在pgsql用户下初始化数据库

[pgsql@xiaoran bin]$ ./initdb -E utf8 -D /usr/local/pgsql/data/
The files belonging to this database system will be owned by user "pgsql".
This user must also own the server process.

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

Data page checksums are disabled.

fixing permissions on existing directory /usr/local/pgsql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default timezone ... Asia/Shanghai
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... 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:

    ./pg_ctl -D /usr/local/pgsql/data/ -l logfile start

6.添加pgsql环境变量,并启动pgsql数据,为了方便可以吧pgsql做成服务启动(自行百度)

vim ~/.bash_profile
#在环境变量中加下面这句话
/usr/local/pgsql/bin   source ~/.bash_profile
#启动Pgsql数据
[pgsql@xiaoran bin]$ ./pg_ctl -D /usr/local/pgsql/data/ -l /usr/local/pgsql/log/pgsql.log start
waiting for server to start.... done
server starte

7.登陆数据

psql template1 或 /usr/local/pgsql/bin/psql template1

注意:默认登陆一定要指定数据库,否则会报错

[pgsql@xiaoran bin]$ ./psql
psql.bin: FATAL:  database "pgsql" does not exist

8.pgsql数据库帮助

template1=# ?
General
  copyright             show PostgreSQL usage and distribution terms
  crosstabview [COLUMNS] execute query and display results in crosstab
  errverbose            show most recent error message at maximum verbosity
  g [FILE] or ;         execute query (and send results to file or |pipe)
  gexec                 execute query, then execute each value in its result
  gset [PREFIX]         execute query and store results in psql variables
  gx [FILE]             as g, but forces expanded output mode
  q                     quit psql
  watch [SEC]           execute query every SEC seconds

Help
  ? [commands]          show help on backslash commands
  ? options             show help on psql command-line options
  ? variables           show help on special variables
  h [NAME]              help on syntax of SQL commands, * for all commands

Query Buffer
  e [FILE] [LINE]       edit the query buffer (or file) with external editor
  ef [FUNCNAME [LINE]]  edit function definition with external editor
  ev [VIEWNAME [LINE]]  edit view definition with external editor
  p                     show the contents of the query buffer
  
                     reset (clear) the query buffer
  s [FILE]              display history or save it to file
  w FILE                write query buffer to file

Input/Output
  copy ...              perform SQL COPY with data stream to the client host
  echo [STRING]         write string to standard output
  i FILE                execute commands from file
  ir FILE               as i, but relative to location of current script
  o [FILE]              send all query results to file or |pipe
  qecho [STRING]        write string to query output stream (see o)

Conditional
  if EXPR               begin conditional block
  elif EXPR             alternative within current conditional block
  else                  final alternative within current conditional block
  endif                 end conditional block

Informational
  (options: S = show system objects, + = additional detail)
  d[S+]                 list tables, views, and sequences
  d[S+]  NAME           describe table, view, sequence, or index
  da[S]  [PATTERN]      list aggregates
  dA[+]  [PATTERN]      list access methods
  db[+]  [PATTERN]      list tablespaces
  dc[S+] [PATTERN]      list conversions
  dC[+]  [PATTERN]      list casts
  dd[S]  [PATTERN]      show object descriptions not displayed elsewhere
  dD[S+] [PATTERN]      list domains
  ddp    [PATTERN]      list default privileges
  dE[S+] [PATTERN]      list foreign tables
  det[+] [PATTERN]      list foreign tables
  des[+] [PATTERN]      list foreign servers
  deu[+] [PATTERN]      list user mappings
  dew[+] [PATTERN]      list foreign-data wrappers
  df[antw][S+] [PATRN]  list [only agg/normal/trigger/window] functions
  dF[+]  [PATTERN]      list text search configurations
  dFd[+] [PATTERN]      list text search dictionaries
  dFp[+] [PATTERN]      list text search parsers
  dFt[+] [PATTERN]      list text search templates
  dg[S+] [PATTERN]      list roles
  di[S+] [PATTERN]      list indexes
  dl                    list large objects, same as lo_list
  dL[S+] [PATTERN]      list procedural languages
  dm[S+] [PATTERN]      list materialized views
  dn[S+] [PATTERN]      list schemas
  do[S]  [PATTERN]      list operators
  dO[S+] [PATTERN]      list collations
  dp     [PATTERN]      list table, view, and sequence access privileges
  drds [PATRN1 [PATRN2]] list per-database role settings
  dRp[+] [PATTERN]      list replication publications
  dRs[+] [PATTERN]      list replication subscriptions
  ds[S+] [PATTERN]      list sequences
  dt[S+] [PATTERN]      list tables
  dT[S+] [PATTERN]      list data types
  du[S+] [PATTERN]      list roles
  dv[S+] [PATTERN]      list views
  dx[+]  [PATTERN]      list extensions
  dy     [PATTERN]      list event triggers
  l[+]   [PATTERN]      list databases
  sf[+]  FUNCNAME       show a function's definition
  sv[+]  VIEWNAME       show a view's definition
  z      [PATTERN]      same as dp

Formatting
  a                     toggle between unaligned and aligned output mode
  C [STRING]            set table title, or unset if none
  f [STRING]            show or set field separator for unaligned query output
  H                     toggle HTML output mode (currently off)
  pset [NAME [VALUE]]   set table output option
                         (NAME := {border|columns|expanded|fieldsep|fieldsep_zero|
                         footer|format|linestyle|null|numericlocale|pager|
                         pager_min_lines|recordsep|recordsep_zero|tableattr|title|
                         tuples_only|unicode_border_linestyle|
                         unicode_column_linestyle|unicode_header_linestyle})
  	 [on|off]            show only rows (currently off)
  T [STRING]            set HTML <table> tag attributes, or unset if none
  x [on|off|auto]       toggle expanded output (currently off)

Connection
  c[onnect] {[DBNAME|- USER|- HOST|- PORT|-] | conninfo}
                         connect to new database (currently "template1")
  conninfo              display information about current connection
  encoding [ENCODING]   show or set client encoding
  password [USERNAME]   securely change the password for a user

Operating System
  cd [DIR]              change the current working directory
  setenv NAME [VALUE]   set or unset environment variable
  	iming [on|off]       toggle timing of commands (currently off)
  ! [COMMAND]           execute command in shell or start interactive shell

Variables
  prompt [TEXT] NAME    prompt user to set internal variable
  set [NAME [VALUE]]    set internal variable, or list all if no parameters
  unset NAME            unset (delete) internal variable

Large Objects
  lo_export LOBOID FILE
  lo_import FILE [COMMENT]
  lo_list
  lo_unlink LOBOID      large object operations

9.停止数据

./pg_ctl -D /usr/local/pgsql/data/ -m smart -W -t SECS -s stop

启动、停止、重启 延伸资料

[pgsql@xiaoran bin]$ ./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    [-D DATADIR] [-l FILENAME] [-W] [-t SECS] [-s]
                  [-o OPTIONS] [-p PATH] [-c]
  pg_ctl stop     [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s]
  pg_ctl restart  [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s]
                  [-o OPTIONS] [-c]
  pg_ctl reload   [-D DATADIR] [-s]
  pg_ctl status   [-D DATADIR]
  pg_ctl promote  [-D DATADIR] [-W] [-t SECS] [-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             wait until operation completes (default)
  -W, --no-wait          do not wait until operation completes
  -?, --help             show this help, then exit
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=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 (default)
  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 <pgsql-bugs@postgresql.org>.
原文地址:https://www.cnblogs.com/Roobbin/p/13992955.html