POSTGRESQL 无法创建数据库 出现类似createdb: could not connect

作者:袖梨 2022-06-29

运行下面的命令创建名为mydb的数据库

 代码如下 复制代码

createdb mydb

得到一个错误:psql: FATAL: role "terry" does not exist, terry是我的机器名, 在postgresql数据库中没有名为terry的role, 所以会报出这个错误, 只要加上-U参数来指定一个存在的role就可以了,所以我将上面的命令改为:

 代码如下 复制代码

createdb mydb -U postgres

又得到一个错误:psql: FATAL:  Peer authentication failed for user "postgres",

解决办法如下:

1. 运行下面的命令编辑pg_hba.conf文件

 代码如下 复制代码

sudo gedit /etc/postgresql/9.1/main/pg_hba.conf

2. 将

 代码如下 复制代码

# Database administrative login by Unix domain socket
local   all             postgres                                peer

改为

# Database administrative login by Unix domain socket
local   all             postgres                                trust

3. 保存后执行下面的命令重新加载配置文件:

 代码如下 复制代码

sudo /etc/init.d/postgresql reload

再执行 createdb mydb1 -U postgres , 已经成功的创建了mydb1数据库.


看一老外的文件没译,各位有需要可参考。

I figured I’d share getting setup on my Ubuntu machine.

Installing

sudo apt-get install postgresql
Creating User

createdb book
createuser: could not connect to database postgres: FATAL:  role "myusername" does not exist


The bit above can be resolved with the following (replacing myusername for yours)

sudo -u postgres createuser myusername
Shall the new role be a superuser? (y/n) y


Then this should work

createdb book


Installing Extensions
There should be more problems when trying to create the 5 extensions

psql book -c "CREATE EXTENSION tablefunc"
ERROR:  could not open extension control file "/usr/share/postgresql/9.1/extension/tablefunc.control": No such file or directory


You can fix it by just downloading a package.

sudo apt-get install postgresql-contrib


Downloading the postgresql-contrib packages will give the ability to use the following five commands:

psql book -c "CREATE EXTENSION tablefunc"
psql book -c "CREATE EXTENSION fuzzystrmatch"
psql book -c "CREATE EXTENSION pg_trgm"
psql book -c "CREATE EXTENSION cube"
psql book -c "CREATE EXTENSION dict_xsyn"


And now you should be good to go!

Thanks to the post by Tanner Watson for the CREATE EXTENSION command

相关文章

精彩推荐