简体   繁体   English

在不使用密码且不使用sudo -u postgres的情况下运行psql和pg_dump

[英]Run psql and pg_dump without password and without using sudo -u postgres

I am trying to copy one table from one database to another: 我正在尝试将一个表从一个数据库复制到另一个数据库:

sudo -u postgres pg_dump -t _stats db30 | psql db8;

However, I always get asked for a password, and I do not know what it is. 但是,总是会要求我输入密码,我不知道它是什么。 Is there a way to do this without pg_dump? 没有pg_dump,有没有办法做到这一点? Is there a way so that I can not be asked for a password when I run psql ? 有没有办法让我在运行psql时不要求输入密码?

Note, to get into postgres I have to run sudo -u postgres psql instead of psql 注意,要进入postgres,我必须运行sudo -u postgres psql而不是psql

User management and permission on a postgres server is a complex topic, but you have probably only a server installed on your desktop and use it only on localhost, so security is not so important. Postgres服务器上的用户管理和权限是一个复杂的主题,但是您可能只有一台服务器安装在桌面上,并且只能在localhost上使用,因此安全性并不是那么重要。

You have to do 3 steps: 您必须执行3个步骤:

1) Edit the pg_hba.conf file and restart the server 1)编辑pg_hba.conf文件并重新启动服务器

2) Login with psql and set a password for the user postgres 2)使用psql登录并为用户postgres设置密码

3) Edit (or create) the file ~/.pgpass 3)编辑(或创建)文件〜/ .pgpass

NOTE: you could use the authentication method trust in pg_hba.conf and avoid the step 2 and 3, but this is really TOO permissive, and you shouldn't use it, even on localhost. 注意:您可以使用的身份验证方法trust pg_hba.conf中,避免步骤2和3,但是这真的是宽容了,你不应该使用它,即使在本地主机上。

The pg_hba.conf file pg_hba.conf文件

To understand the file pg_hba.conf please read here: http://www.postgresql.org/docs/9.4/static/auth-pg-hba-conf.html 要了解pg_hba.conf文件,请在此处阅读: http : //www.postgresql.org/docs/9.4/static/auth-pg-hba-conf.html

Basically, if you server is on localhost and security does not matter, you can simply allow all user to connect with md5 authentication method. 基本上,如果您的服务器位于localhost上并且安全性无关紧要,则可以简单地允许所有用户使用md5身份验证方法进行连接。 If you don't know, where this file is, use this command: 如果您不知道此文件在哪里,请使用以下命令:

locate pg_hba.conf

Probably is in /etc/postgresql/9.3/main/pg_hba.conf or similar. 可能在/etc/postgresql/9.3/main/pg_hba.conf或类似文件中。

Edit the file and change the already existing lines so (at end of the file): 编辑文件并更改已经存在的行,以便(在文件末尾):

local   all             all                                     md5
host    all             all             127.0.0.1/32            md5

Now restart the server with 现在使用以下命令重新启动服务器

sudo service postgresql restart

Set a password for the user postgres 为用户postgres设置密码

First login in psql: 首次登录psql:

sudo -u postgres psql

Now, within psql, change the password: 现在,在psql中,更改密码:

ALTER USER postgres PASSWORD 'your-password';

The pgpass file pgpass文件

Now you can login in psql with psql -U postgres (without sudo -u postgres ) but have to enter the password. 现在,您可以使用psql -U postgres (不使用sudo -u postgres )登录psql,但必须输入密码。 To avoid to digit the password every time, you can set up the pgpass file. 为了避免每次都输入密码,您可以设置pgpass文件。 If does not already exist, you must create a file named .pgpass in your home directory. 如果尚不存在,则必须在主目录中创建一个名为.pgpass的文件。 The file must be owned by your user and be readable only by your user: 该文件必须归您的用户所有,并且只能由您的用户读取:

chown $USER:$USER ~/.pgpass
chmod 600 ~/.pgpass

Now write in the file those lines: 现在在文件中写以下行:

localhost:5432:*:postgres:your-password
127.0.0.1:5432:*:postgres:your-password

Alternately you can use the environment variable PGPASSWORD : http://www.postgresql.org/docs/9.4/static/libpq-envars.html 或者,您可以使用环境变量PGPASSWORDhttp : //www.postgresql.org/docs/9.4/static/libpq-envars.html

Ready. 准备。 Now you can login in postgres with psql -U postgres without enter the password. 现在,您可以使用psql -U postgres登录到postgres,而无需输入密码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM