从Google Colab连接到Postresql数据库

从Google Colab连接到Postresql数据库

问题描述:

我正在尝试将我的Google Colab连接到我的Postgres数据库。当我尝试从Jupyter Notebook连接到它时,它正在工作,因此我猜测我的凭据很好。这是我得到的错误:

I am trying to connect my Google Colab to my Postgres DB. When I try to connect to from Jupyter Notebook it's working, so I'm guessing that my credentials are fine. This is the error that I got:


无法连接到服务器:没有这样的文件或目录

could not connect to server: No such file or directory

服务器本地运行并在Unix域套接字上接受
连接 /var/run/postgresql/.s.PGSQL.5432?

Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

我该如何解决?我的Postgres在我的机器上运行。

How can I solve it? My Postgres running on my machine.

谢谢!

您还可以安装PostgreSQL

You can also install PostgreSQL in Colab.

# install
!apt install postgresql postgresql-contrib &>log
!service postgresql start
!sudo -u postgres psql -c "CREATE USER root WITH SUPERUSER"
# set connection
%load_ext sql
%config SqlMagic.feedback=False 
%config SqlMagic.autopandas=True
%sql postgresql+psycopg2://@/postgres

然后您可以使用%sql %% sql magic

Then you can query using %sql or %%sql magic

df = %sql SELECT * FROM pg_catalog.pg_tables
df