Postgresql – Visual Studio Code Postgres SQL Connection

postgresqlvisual studio

I would like to use Visual Studio Code to preform my PostgreSQL queries, and general DB maintenance as an alternative to pgAdmin4. I have read the documentation included with the appropriate extention (PostgreSQL by MS). When I try to Create A New Profile I get the following prompt:

Server Name Prompt

I am working from an Apple Mac so I attempted localhost at first. That did not work. I tried the ip address of the local computer with the same result. I attempted just giving the name of the DB, but that did not work either. Any suggestions as to what I can try next. I am terrible at spelling and rembering names so I would like some form of IDE support when I am working.

This is the error I get:
PostgreSQL: Failed to connect: could not translate host name "localhost\Local" to address: nodename nor servname provided, or not known

Best Answer

As per documentation here Npgsql has a Visual Studio extension (VSIX) which integrates PostgreSQL access into Visual Studio. It allows connecting to PostgreSQL from within Visual Studio's Server Explorer, create an Entity Framework 6 model from an existing database, etc. The extension can be installed directly from the Visual Studio Marketplace page.

The VSIX doesn't automatically add Npgsql to your GAC, App.config, machines.config or any other project or system-wide resource. It only allows accessing PostgreSQL from Visual Studio itself.

As here you van find out, How to Connect Visual Studio LightSwitch to PostgreSQL with dotConnect for PostgreSQL Data Provider.

As per postgresql documentation here Could not translate host name localhost, service 5432 to address: nodename nor servname provided, or not known.

Usually this error is caused by broken /etc/hosts file. The problem could be a missing localhost entry, syntax errors or incorrect whitespace.

For reference, here is what this file should look like by default on macOS:

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1   localhost
255.255.255.255 broadcasthost
::1             localhost 

For further you ref here, here, here and here