一、首先创装个posgrsql吧, 我的系统是ubuntu16.04的,
sudo apt-get install postgresql postgresql-contrib
也许在这之前,使用sudo apt-get update
但是我上来就碰壁了
➜ ~ psql -Upostgrespsql: FATAL: Peer authentication failed for user "postgres"
排除个人尴尬的毛病,继续前进
我们需要更改一个配置文件pg_hba.conf, 这个文件是大多数情况下是在这个文件中
/etc/postgresql/9.5/main
postgresql数据库版本不同路径也不一样, 修改一下9.5就行了;好了, 开始修改了
sudo vim /etc/postgresql/9.5/main/pg_hba.conf
找到类似下面代码的东西
# Database administrative login by Unix domain socketlocal all postgres peer# TYPE DATAbase USER ADDRESS METHOD# "local" is for Unix domain socket connections onlylocal all all peer# IPv4 local connections:host all all 127.0.0.1/32 md5# IPv6 local connections:host all all ::1/128 md5建个数据库吧
把为打#号的第一行和第三行修改peer和md5,修改成trust ,这样:
# Database administrative login by Unix domain socketlocal all postgres trust# TYPE DATAbase USER ADDRESS METHOD# "local" is for Unix domain socket connections onlylocal all all peer# IPv4 local connections:host all all 127.0.0.1/32 trust# IPv6 local connections:host all all ::1/128 md5# Allow replication connections from localhost, by a user with the
改完之后,咱们庆祝一下,重启posgresql
➜ ~ sudo /etc/init.d/postgresql restart[ ok ] Restarting postgresql : postgresql.service.
庆祝完了, 咱们就可以不用密码了
sudo -u postgres psql
咱们看一下用户信息
postgres=# SELECT rolname,rolpassword FROM pg_authid; rolname | rolpassword----------+------------- postgres |
怪不得, 原来没有密码, 咱们设一个吧
postgres=# ALTER USER postgres WITH PASSWORD 'db';ALTER ROLEpostgres=# SELECT rolname,rolpassword FROM pg_authid; rolname | rolpassword----------+------------------------------------- postgres | md5f6ac6d34a0e89b41d18a66cb42318bd9
成功!!!
然后回到把pg_hba.conf改一下,
# Database administrative login by Unix domain socketlocal all postgres md5# TYPE DATAbase USER ADDRESS METHOD# "local" is for Unix domain socket connections onlylocal all all md5# IPv4 local connections:host all all 127.0.0.1/32 md5# IPv6 local connections:host all all ::1/128 md5
重启后,登录一下:
➜ ~ psql -UpostgresPassword for user postgres:psql Type "help" for help.postgres=#
成功!!!!
提示:
如果出现完全卸载重装可以使用这些命令,亲测可行:
sudo apt-get remove postgresqlsudo apt-get purge postgresqlsudo apt-get --purge remove postgresql*sudo apt-get --purge remove postgresql*sudo rm -r /etc/postgresql/sudo rm -r /etc/postgresql-common/sudo rm -r /var/lib/postgresql/sudo rm -r /var/run/postgresuserdel -r postgressudo userdel -r postgressudo apt-get install postgresql
二、 创建一个用户: documentation: 9.5: CREATE USER
create user zm with superuser password 'db';
创建一个user用户,赋予superuser权限, 密码设置为db
三、使用zm用户登录, 创建个数据库, psql9.5文档:documentation: 9.5: SQL Commands
postgres=# create database tensor ;CREATE DATAbase