简体   繁体   English

Postgres开始问题

[英]Postgres starting issue

I'm having an issue with PostgreSQL 我在PostgreSQL上遇到问题

anytime I run 每当我跑步

psql -h localhost

I get 我懂了

psql: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?

A. First make sure PostgreSQL server has been started to remote server. 答:首先确保PostgreSQL服务器已启动到远程服务器。

# /etc/init.d/postgresql start

If it is running and you get above error, you need to add enable TCP/IP support. 如果它正在运行并且出现以上错误,则需要添加启用TCP / IP支持。 By default, the PostgreSQL server only allows connections to the database from the local machine or localhost. 默认情况下,PostgreSQL服务器仅允许从本地计算机或本地主机到数据库的连接。 This is a security feature. 这是一项安全功能。

Step # 1: Allow remote IP address to access PostgreSQL 步骤1:允许远程IP地址访问PostgreSQL

You need to open file called /var/lib/pgsql/data/pg_hba.conf. 您需要打开名为/var/lib/pgsql/data/pg_hba.conf的文件。 Login as postgres user using su command: 使用su命令以postgres用户身份登录:

$ su - postgres
$ vi /var/lib/pgsql/data/pg_hba.conf

Now append following line. 现在添加以下行。 Let us say you would like to give access to 192.168.1.0/24 network: 假设您要授予对192.168.1.0/24网络的访问权限:

host all all 192.168.1.0 255.255.255.0 trust

Please replace 192.168.1.0 and 255.255.255.0 to reflect the actual network IP address range of the clients system in your own network. 请替换192.168.1.0和255.255.255.0,以反映您自己网络中客户端系统的实际网络IP地址范围。

Save close the file. 保存关闭文件。

Step # 2: Allow communication over TCP/IP 步骤2:允许通过TCP / IP进行通信

You need to open PostgreSQL configuration file /var/lib/pgsql/data/postgresql.conf 您需要打开PostgreSQL配置文件/var/lib/pgsql/data/postgresql.conf

$ vi /var/lib/pgsql/data/postgresql.conf

Now bind and open TCP/IP port by setting tcpip_socket to true: 现在,通过将tcpip_socket设置为true来绑定并打开TCP / IP端口:

tcpip_socket = true

Save and close the file. 保存并关闭文件。

Step # 3: Restart PostgreSQL server 步骤#3:重新启动PostgreSQL服务器

Restart the PostgreSQL server with the following command 使用以下命令重新启动PostgreSQL服务器

# /etc/init.d/postgresql restart

This will open default port 5432. 这将打开默认端口5432。

Step # 4: Test your setup 步骤#4:测试设置

Use psql command from client system as follows: 从客户端系统使用psql命令,如下所示:

psql -h PostgreSQL-IP-ADDRESS -U USERNAME -d DATABASENAME

Connect to remote server by IP address 192.168.1.5 and login using vivek user to connect to sales database, use: 通过IP地址192.168.1.5连接到远程服务器,并使用vivek用户登录以连接到销售数据库,请使用:

$ psql -h 192.168.1.5 -U vivek -d sales

Where, 哪里,

-h 192.168.1.5 : Specifies the host name of the machine or IP address (192.168.1.5) on which the server is running. -h 192.168.1.5:指定运行服务器的计算机的主机名或IP地址(192.168.1.5)。

-U vivek : Connect to the database as the vivek username instead of the default. -U vivek:以vivek用户名而不是默认用户名连接到数据库。 You must have account and permission to connect as vivek user. 您必须具有帐户和权限才能以vivek用户身份进行连接。

-d sales : Specifies the name of the database (sales) to connect to. -d sales:指定要连接的数据库(sales)的名称。

For anyone reading this and using Postgres.app, you may need host: localhost in your database.yml. 对于阅读本文并使用Postgres.app的任何人,您可能需要database.yml中的host:localhost。 http://postgresapp.com/documentation#toc_3 http://postgresapp.com/documentation#toc_3

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

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