使用apt安装postgresql ,目前默认的版本是postgresql(9.3.21)
- 习惯性升级apt-update: sudo apt update
- apt无脑安装postgresql:sudo apt install postgresql (安装以后psql会默认一个用户“postgres”,密码是随机的,不用担心,下面会教如何修改密码)
- 登录到postgresql里:sudo -u postgres psql
- 你会看到“postgres=#” 这个就是代表已经进入pg数据库了,可以输入各种命令了
- 修改psql的密码:ALTER USER postgres WITH PASSWORD 'xxxx'; (其中'xxxx'为用户'postgres'的密码,目前postgres的密码修改完毕)
- 退出数据库,修改数据库的配置文件 :\q
- sudo vim /etc/postgresql/9.3/main/postgresql.conf 需要修改的内容如下:
- #listen_addresses = 'localhost' 改为 listen_addresses = '*'
- #password_encryption = on 改为 password_encryption = on
- sudo vim /etc/postgresql/9.3/main/pg_hba.conf 需要添加的内容如下
- host all all 0.0.0.0 0.0.0.0 md5
- 重启pgsql:/etc/init.d/postgresql restart
- 干掉防火墙:iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 5432 -j ACCEPT
-
登录postgre SQL数据库:psql -U postgres -h 127.0.0.1
-
创建新用户hotpot,但不给建数据库的权限 :postgres=# create user "hotpot" with password '123456' nocreatedb; 用户名处是双引号
-
建立数据库,并指定所有者:postgres=#create database "testdb" with owner = "hotpot";
FAQ:一定要注意单引号还是双引号,每个sql语句都要有分号结尾!!