Connect to a database over a unix socket using SQLAlchemy - postgresql

I'm trying to connect to my Cloud SQL DB using SQLAlchemy from my cloud function but I can't seem to work out the correct connection string.
DATABASE_URL=postgres://$DB_USER:$_DB_PWD#/$DB_NAME?unix_socket=/cloudsql/$DB_INSTANCE
Which gives me the error:
pyscopg2.ProgrammingError: invalid dns: invalid connection option "unix_socket"
What is the correct way to connect to a Postgresql 9.6 DB over a unix socket using pyscopg2?

The special keyword needed here is host:
DATABASE_URL=postgres://user:password#/dbname?host=/path/to/db
Note that the path in host should be a path, not the socket file itself (psycopg2 assumes the socket has the standard naming convention .s.PGSQL.5432)
https://docs.sqlalchemy.org/en/latest/dialects/postgresql.html#unix-domain-connections

Related

Error to connect Postgresql DB from Oracle dblink using Oracle Database Gateway

I'm trying to connect from Oracle Database (dblink) to Postgresql database use Oracle Database Gateway (dg4msql 19), on the Oracle Database Gateway for MS SQL Server all work fine. On the Oracle DB create dblink, but when try to connect get Error message:
ORA-28500 Oracle ODBC SQL Server Wire Protocol driver Socket closed
08S01 Oracle ODBC SQL Server Wire protocol driver
The server does not support SSL {HY000} !
listener work, tnsnames.ora also give correct answer. May be in initdg4msql.ora file I have to use some string to connect without SSL?
HS_FDS_CONNECT_INFO = postgre_servet.net:5432//database - I don`t known that is correct for PostgreSQL, or for PostgreSQL should use only ODBC connection?
How correctly use HS_FDS_CONNECT_INFO = postgre_servet.net:5432//database, or for PostgreSQL should I use only ODBC connection?
The PostgreSQL server is not configured to accept SSL connections.
First, verify that PostgreSQL was built with SSL support: that can be seen from the output of pg_config --configure (should contain --with-openssl).
To configure SSL support, set ssl = on in postgresql.conf and provide a server certificate and private key (parameters ssl_vert_file and ssl_key_file). Reload PostgreSQL to activate.
For details, read the documentation.

Is it possible to use an instance name instead of a port number in PostgreSQL?

I am migrating from SQL Server to PostgreSQL. I found out that in PostgreSQL you have ports which allow you to connect to your server. I guess this port is equivalent to SQL Server instance (e.g. MSSQLSERVER), but in SQL Server you can choose any instance name you want, unlike PostgreSQL which uses only port numbers.
Can I use instance names or something similar for PostgreSQL instead of port numbers?

How can I call a postgresql db stored in DigitalOcean from ASP.NET Core web api?

I'm tying to use the connection string provided by DigitalOcean directly on the appsettings.json file, but shows error
ArgumentException: Keyword not supported: postgresql://XXXXX_user:xxxxxxxxxxxxx#db-trackbus-nyc3-xxxx-do-user-xxxxx-0.b.db.ondigitalocean.com:xxxxx/trackbus?sslmode (Parameter 'keyword')
Npgsql.NpgsqlConnectionStringBuilder.GetProperty(string keyword)
This is the exact connection string provided by DigitalOcean:
postgresql://xxxx_user:xxxxx#db-trackbus-nyc3-xxxx-do-user-xxxxx-0.b.db.ondigitalocean.com:xxxxx/TrackBus?sslmode=require
I'm using Npgsql nuget package, when the db was local was working fine using th regular connection string
"Server=xxx.0.0.x;Port=xxxx;Database=TrackBus;User Id=postgres;Password=xxxx;"
I've also tried this (private and public):
"Server=private-db-trackbus-nyc3-xxxxx-do-user-xxxxx-0.b.db.ondigitalocean.com;Port=xxxxx;Database=TrackBus;User Id=xxxxx_user;Password=xxxxxxxxxx;Integrated Security=true;"
Error:
An unhandled exception occurred while processing the request.
PostgresException: 28000: no pg_hba.conf entry for host "xxx.xxx.xxx.xxx", user "xxxx", database "TrackBus", SSL off
I searched this last 28000 exception but couldn't apply any solution found.
The connection string provided by DigitalOcean is for CLI:
postgresql://xxxx_user:xxxxx#db-trackbus-nyc3-xxxx-do-user-xxxxx-0.b.db.ondigitalocean.com:xxxxx/TrackBus?sslmode=require
You have to convert it to the format that appsettings.json uses and it will be like this:
"Server=db-trackbus-nyc3-xxxx-do-user-xxxxx-0.b.db.ondigitalocean.com;Port=xxxxx;Database=TrackBus;User Id=xxxxxxxx;Password=xxxxxxxxx;sslmode=Require;Trust Server Certificate=true;"
The sslmode=Require;Trust Server Certificate=true; is super important because the managed database in DigitalOcean has it that that one.
Also remember that all postgresql database by default are blocking remote connection, you have to add the permission on the pg_hba.conf file from the Postgresql installation folder.

How to make pg-promise use peer authentication?

I'm using pg-promise in a typescript express app.
I can log on to psql by just typing psql because the linux username is the same as the database name and I'm set to use peer authentication.
I can't seem to get pg-promise to let me do this - it always fails and gives an error that password authentication failed.
I've been looking at the pgpromise docs but can't find anything - I've tried all sorts of connection strings.
I managed to make this work for me by using a Unix Domain Socket instead of a TCP socket and connecting using a connection string.
So my connection looks like this const db = pgp('socket:/var/run/postgresql'); but presumably the /var/run/postgresql could vary by system.

Azure PostgreSQL foreign data wrapper postgres_fdw

I am trying to use the PostgreSQL foreign data wrapper (postgres_fdw) extension from an Azure DB for PostgreSQL instance. The foreign data wrapper should be supported as mentioned in Azure DB for PostgreSQL: https://learn.microsoft.com/en-us/azure/postgresql/concepts-extensions
I manage to create a server and a mapping for the user, but when I try to import a table or a schema I have the following error:
ERROR: could not connect to server "<server with public hostname on AWS>"
DETAIL: could not translate host name "<server with public hostname on AWS>" to address: Unknown host
When replacing the hostname with a public IP I get the following:
ERROR: could not connect to server "<SERVER NAME>"
DETAIL: could not connect to server: Network is down (0x00002742/10050)
Is the server running on host "<public IP of the server" and accepting
TCP/IP connections on port 5432?
In the firewall options of the Azure DB for PostgreSQL instance, I already have the AllowAllIps rule starting 0.0.0.0 and ending 255.255.255.255. is there any thing else to configure to get access to servers outside of Azure, DNS or firewall configuration?
Not entirely sure about this, but we were trying to do something like this the other day, and stumbled upon this (somewhat disheartening) quote:
"Currently, outbound connections from Azure Database for PostgreSQL are
not supported, except for connections to other Azure Database for
PostgreSQL servers."
Source: https://learn.microsoft.com/en-us/azure/postgresql/concepts-extensions#dblink-and-postgres_fdw