Postgres启用TCP / IP连接

时间:2014-12-31 07:40:06

标签: ruby-on-rails postgresql tcp centos postgresql-9.4

我在CentOS 6.6系统上安装了postgres-9.4数据库服务器。我正在尝试从我的笔记本电脑连接到此服务器(连接到同一网络。笔记本电脑的IP是192.168.1.105。我正在运行psql -U postgres -h 192.168.1.52)。 psql命令失败并显示错误消息:

psql -U postgres -h 192.168.1.52
could not connect to server: COnnection refused
Is the server running on host 192.168.1.52 and accepting TCP/IP connection on port 5432?

我的配置: /var/lib/pgsql/9.4/data/pg_hba.conf

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
host    all             all             192.168.0.0/16          trust
host    all             all             0.0.0.0/0               trust
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                peer
#host    replication     postgres        127.0.0.1/32            ident
#host    replication     postgres        ::1/128                 ident

的/ etc / SYSCONFIG / iptables的:

# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
-A INPUT -m state --state NEW -m tcp -p tcp --dport 5432 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
COMMIT

我已经重新启动了iptables服务。

/var/lib/pgsql/9.4/data/postgresql.conf

listen_addresses = '*'
port = 5432

我重启服务器如下:

[root@cinch-database1 9.4]# service postgresql-9.4 restart
Stopping postgresql-9.4 service:                           [  OK  ]
Starting postgresql-9.4 service:                           [  OK  ]

ps aux和grep postgres返回以下内容:

ps auxwww | grep postgres
postgres 11460  0.0  0.0 325096 14860 ?        S    01:36   0:00 /usr/pgsql-9.4/bin/postmaster -D /var/lib/pgsql/9.4/data
postgres 11463  0.0  0.0 180244  1264 ?        Ss   01:36   0:00 postgres: logger process                                
postgres 11465  0.0  0.0 325096  1564 ?        Ss   01:36   0:00 postgres: checkpointer process                          
postgres 11466  0.0  0.0 325096  2544 ?        Ss   01:36   0:00 postgres: writer process                                
postgres 11467  0.0  0.0 325096  1496 ?        Ss   01:36   0:00 postgres: wal writer process                            
postgres 11468  0.0  0.0 325508  2340 ?        Ss   01:36   0:00 postgres: autovacuum launcher process                   
postgres 11469  0.0  0.0 180376  1476 ?        Ss   01:36   0:00 postgres: stats collector process                       
root     11516  0.0  0.0 103252   844 pts/0    S+   01:41   0:00 grep postgres

当我在服务器上执行netstat并且为5432执行grep时,我什么也得不到:

netstat | grep 5432

netstat -tulnp会返回此信息:

tcp        0      0 0.0.0.0:5432                0.0.0.0:*                   LISTEN      13227/postmaster 

输出'netstat -l | grep postgres'

[root@cinch-database1 pg_log]# netstat -l|grep postgres
tcp        0      0 *:postgres                  *:*                         LISTEN      
tcp        0      0 *:postgres                  *:*                         LISTEN  

但如前所述,我的笔记本电脑的psql无法连接到服务器。

我错过了什么?

2 个答案:

答案 0 :(得分:1)

这是一个防火墙问题。我必须在序列中的前面的iptables文件中移动以下两行:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 5432 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

新iptables:

# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 5432 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

在此之后我重启了iptables服务:

service iptables restart

现在我可以从笔记本电脑连接到postgres。

答案 1 :(得分:0)

防火墙似乎阻止了您的连接,因此只需尝试使用以下命令禁用防火墙:

sudo service iptables stop

然后如果通过,请正确设置。