简体   繁体   English

与 pgAdmin 的远程 PostgreSQL 连接

[英]Remote PostgreSQL connection with pgAdmin

I'm trying to set up a remote connection through PostgreSQL running on my server , based on Ubuntu 16.04.我正在尝试通过基于 Ubuntu 16.04 的服务器上运行的 PostgreSQL 设置远程连接。 So far, when I click on the Save button on pgAdmin, it sort of freezes, does nothing.到目前为止,当我点击 pgAdmin 上的 Save 按钮时,它有点冻结,什么也不做。 After typing .../manage.py runserver My_droplet_IP:5432, I try the webpage, and it is accessible.输入.../manage.py runserver My_droplet_IP:5432后,我试了一下网页,可以访问。

I followed this tutorial after creating my droplet.创建 Droplet 后,我​​遵循了本教程。 https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql-on-ubuntu-16-04 https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql-on-ubuntu-16-04

Then I edited the settings.py;然后我编辑了settings.py; pg_hba.conf; pg_hba.conf; postgresql.conf files postgresql.conf 文件

settings.py:

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresqlpsycopg2',
'NAME': '.....',
'USER': '....',
'PASSWORD': '....',
'HOST': '127.0.0.1',
'PORT': '5432',

STATICROOT = os.path.join(BASE_DIR, 'static/') - at the end of the page

And, ofcourse changed the ALLOWED HOSTS = ['....'] with my droplet ip aswell.而且,当然也用我的 droplet ip 更改了 ALLOWED HOSTS = ['....'] 。

postgresql.conf listen_address is set to '*'

pg_hba.conf file: pg_hba.conf 文件:

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

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             0.0.0.0/0               md5
# IPv6 local connections:
host    all             all             ::1/128                 md5

Also allowed firewall, and made an exception to 5432 to be allowed.也允许防火墙,并破例5432被允许。

Any ideas?有任何想法吗?

First of all test if you can connect to the database via psql :首先测试是否可以通过psql连接到数据库:

psql -h ip_address -d name_of_the_database -U username

If you get connection refused error you had to set up something wrong and check the What should I check if remote connect to PostgreSQL not working?如果您收到连接拒绝错误,您必须设置错误并检查如果远程连接到 PostgreSQL 不起作用我应该检查什么?

psql: could not connect to server: Connection refused Is the server running on host ip_address

What should I check if remote connect to PostgreSQL not working?如果远程连接到 PostgreSQL 不起作用,我应该检查什么?

  1. Check the authentication configuration in pg_hba.conf检查pg_hba.conf的身份验证配置

    Usually located on linux - /etc/postgresql/version/main/pg_hba.conf .通常位于 linux - /etc/postgresql/version/main/pg_hba.conf You should allow authentication for client for specific IP all from all IP addresses:您应该允许对来自所有 IP 地址的特定 IP 的客户端进行身份验证:

     # Database administrative login by Unix domain socket local all postgres peer # TYPE DATABASE USER ADDRESS METHOD # "local" is for Unix domain socket connections only local all all peer # IPv4 local connections: host all all 0.0.0.0/0 md5 # IPv6 local connections: host all all ::0/0 md5 #all ips host all all all md5

    More information how to set up pg_hba.conf you can find in documentation .有关如何设置pg_hba.conf更多信息,您可以在文档中找到。

  2. Then you should set up listening on specific port.然后你应该设置监听特定端口。

    You have to find the postgresql.conf .你必须找到postgresql.conf Usually located /etc/postgresql/9.1/main/postgresql.conf ) file and change the line with listen_address from:通常位于/etc/postgresql/9.1/main/postgresql.conf ) 文件并更改带有 listen_address 的行:

     #listen_address = ''

    to (don't forget remove # which means comment):到(不要忘记删除 # 这意味着评论):

     listen_address = '*'
  3. After every step you should restart Postgresql service:在每一步之后,您都应该重新启动 Postgresql 服务:

     sudo service postgresql restart
  4. After step 2 you should see port 5432 (or 5433) in listening address after netstat command:在第 2 步之后,您应该在 netstat 命令之后在侦听地址中看到端口 5432(或 5433):

     netstat -ntlp
  5. After that you have to open port for PostgreSQL in firewall:之后,您必须在防火墙中为 PostgreSQL 打开端口:

     sudo ufw allow 5432

    You can check firewall settings with (you should see 5432 in the list):您可以检查防火墙设置(您应该在列表中看到 5432):

     sudo ufw status
  6. If any of the previous step doesn't work you should check if PostgreSQL is not running on different port (usually 5433) and repeat the previous steps.如果前面的任何步骤不起作用,您应该检查 PostgreSQL 是否没有在不同的端口(通常是 5433)上运行并重复前面的步骤。

    This happens very often when you have more running versions of PostgreSQL or you upgrade database and forgot stop the previous version of PostgreSQL.当您有更多正在运行的 PostgreSQL 版本或升级数据库而忘记停止以前版本的 PostgreSQL 时,这种情况经常发生。

If you have problems to find configuration files you can check this thread Where are my postgres *.conf files?如果您在查找配置文件时遇到问题,可以查看此线程我的 postgres *.conf 文件在哪里? . .

如果您使用 GCP,请记住在 GCP 内设置防火墙规则以允许该端口,这可能会节省您几个小时的调试时间。

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

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