如何从Google colaboratory连接到PostgreSQL服务器

时间:2019-04-21 07:00:54

标签: python postgresql google-colaboratory

我想从google colaboratory连接到Postgresql服务器localhost。 但是我有一些问题。

我尝试使用psycopg2连接到服务器。当我使用Pycharm尝试时它起作用了,但是在google colaboratory中却不起作用。 主机是localhost,数据库名称是postgres,用户是postgres,密码是0000。这是我的代码。

import psycopg2

conn_string = "host = 'localhost' dbname = 'postgres' user = 'postgres' password = '0000'"
conn = psycopg2.connect(conn_string)
cur = conn.cursor()

错误消息是

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

我不知道为什么无法连接到服务器。请让我知道问题和解决方案。感谢您的帮助。

0 个答案:

没有答案