Skip to content

Instantly share code, notes, and snippets.

@Casxt
Last active November 28, 2018 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Casxt/c2da3f9730e45f110c42bff914ea20ff to your computer and use it in GitHub Desktop.
Save Casxt/c2da3f9730e45f110c42bff914ea20ff to your computer and use it in GitHub Desktop.
postgresql常用操作

安装

yum install postgresql-server postgresql-contrib
sudo postgresql-setup initdb
sudo systemctl start postgresql
sudo systemctl enable postgresql

默认位置/var/lib/pgsql/{version}/data

配置

alter user postgres with password '123456';

修改pg_hba.conf

host    all             all             127.0.0.1/32            password

修改postgresql.conf取消监听地址和端口的注释

重启

sudo systemctl restart postgresql

验证

psql -U postgres -h 127.0.0.1 -W

常用命令

su - postgres -c "psql"
\password           设置密码。
\q                  退出。
\h                  查看SQL命令的解释,比如\h select。
\?                  查看psql命令列表。
\l                  列出所有数据库。
\c [database_name]  连接其他数据库。
\dn                 列出当前数据库的所有Schema。
\dt public.*        列出当前public Schema的所有表格。
\dt *.*             列出当前数据库的所有表格。
\d [table_name]     列出某一张表格的结构。
\du                 列出所有用户。
\e                  打开文本编辑器。
\conninfo           列出当前数据库和连接的信息。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment