Postgres ODBC connection issue from Ubuntu VM - postgresql

I am attempting to use a Zabbix server running on an Ubuntu virtual machine to monitor the Postgres database in our application running on the same host machine (not a VM). To be clear, I am trying to connect from a Linux Ubuntu virtual machine on my computer to Postgres also running not in a VM on the same computer. Zabbix makes use of ODBC, so a preliminary step in the process is to get the ODBC connection to Postgres working correctly. However, I am having a problem.
Steps I have taken:
installed unixODBC via sudo apt-get install unixodbc unixodbc-dev
installed unixODBC driver for Postgres via sudo apt-get install odbc-postgresql
configured odbc.ini to the following:
[test]
Description = test database
Driver = /usr/lib/x86_64-linux-gnu/odbc/psqlodbca.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libodbcpsqlS.so
Server = 192.168.240.1
User = postgres
Password =
Port = 5432
Database = mydb
Yet when I test the connection via:
isql test -v
I get the following error:
[08001][unixODBC]could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
More notes:
I can successfully connect to Postgres from the admin running on the local (non VM) machine
port 5432 has been completely opened from Windows Firewall on the local machine
telnet to 192.168.240.1 (the network IP of the local machine) on port 5432 succeeds
This all implies that the problem has to do with the ODBC configuration in the Ubuntu VM. I spent several hours searching and trying various things but to no avail. If I can get isql to work correctly, I should be in business, as Zabbix basically sits right on top of ODBC for its database monitoring functions.
Thanks in advance for your help.

I think your configuration options are a little off. Try this:
[test]
Driver = /usr/lib/x86_64-linux-gnu/odbc/psqlodbca.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libodbcpsqlS.so
Database = mydb
Servername = 192.168.240.1
UserName = postgres
Password =
Port = 5432
Protocol = 7.4
Using Servername instead of Server might be sufficient.

I'd recommend the following steps to getting ODBC and PostgreSQL to play together (ignoring the apt install steps, since you already did these):
sudo odbcinst -i -d -f /usr/share/psqlodbc/odbcinst.ini.template
sudo odbcinst -i -s -l -n test -f /usr/share/doc/odbc-postgresql/examples/odbc.ini.template
sudo nano /etc/odbc.ini
Here's what these do:
Sets up your odbcinst.ini file with the files in the right places.
Sets up your odbc.ini file (for the system).
Edits the system odbc.ini file you created in step 2, where you can replace options to match your needs.
If you do not want the odbc.ini file to be system-wide, you can also limit it to just the user if you call step #2 without the -l parameter. In that case, it'll create or modify a ~/.odbc.ini file, which you can edit for your needs.
The unixODBC folks seem to recommend using odbcinst for setting this stuff up, as it knows where to put the files. Unfortunately, to use it to great effect, you'd need to know where to find the drivers' template files for your driver. The paths I've provided here match the ones for the Ubuntu package.

Related

pgAdmin4 + WSL2: can't see dbs created with other client

I have followed the steps from here. I successfuly installed both postgres#12.8 in WSL2 and PGAdmin4 on Windows.
Once I start service in WSL and create a server connection in pgAdmin I am not able to see any databases create from the pgAdmin GUI or neither from psql client.
I tried to:
set env variable PGHOST to localhost
editing postgresql.conf to listen_addresses = 'localhost'
connecting pgAdmin server by localhost name and by IP 127.0.0.1
nothing works..
I just ran into the same issue.
If you try to access your DB installed with pgAdmin from WSL 2, make sure you didnt install postgreSQL from Windows, as sudo service postgresql start from WSL will also start the only postgresql service you need.
Basically, it means you need to install postgresql ( sudo apt-get install postgresql) and start it FROM WSL, and then just open pgAdmin from Windows. Do NOT install postgresql directly on Windows as then your pgAdmin might create your DBs into the postgresql service started in Windows and not from the one started from WSL.
Make also sure that pgAdmin is installed for the same user that your WSL 2 uses, if you use it from VSCode for example. Otherwise, for the same reason, your WSL may not be able to find the corresponding DB.
Note that you can list the DB existing on a specific port by running psql -p 5432 -l from WSL ( change 5432 by the port you want to use ).

How to add Postgres on Mac OS with MAMP to PATH?

I'm following this tutorial on how to run Postgres on MAC OS X with MAMP. But I can't seem to add PostgreSQL to my PATH variable. When I run export PATH="/Library/PostgreSQL/11/bin" I get this error:
prompt_context:13: command not found: whoami
prompt_background_jobs:1: command not found: wc
prompt_background_jobs:2: command not found: awk
Here's exactly what I did:
I downloaded PostgreSQL 11.2 from Enterprise DB
I installed it using the installer:
username: postgres
password: postgres
port: 5433
The PostgreSQL folder can now be found here: /Library/PostgreSQL. Inside there's a folder called 11. Inside /Library/PostgreSQL/11 I can see the bin folder and data folder.
There's another folder inside my Applications folder called PostgreSQL 11. Here I can see a Documentation folder and some other programs I believe:
Application Stack Builder
pgAdmin 4
Reload Configuration
SQL Shell (psql)
The following error appears when I ignore setting the PATH variable and run su postgres:
su: Sorry
or sudo postgres:
"root" execution of the PostgreSQL server is not permitted.
The server must be started under an unprivileged user ID to prevent
possible system security compromise. See the documentation for
more information on how to properly start the server
Also, running psql gives me this error, because PostgreSQL is not included in my environmental variables:
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
Note: this error asks if the server is accepting Unix domain socket with a port 5432, while I installed PostgreSQL on port 5433, what's up with that? Moving to /Library/PostgreSQL/11/bin with cd and running sudo ./psql (should run the Unix Executable file psql) gives me the same error.
Resources that didn't help me any further:
Createuser: could not connect to database postgres: FATAL: role "tom" does not exist
https://dba.stackexchange.com/questions/3005/how-to-run-psql-on-mac-os-x
Psql could not connect to server: No such file or directory, 5432 error?
When you run export PATH="/Library/PostgreSQL/11/bin" you are telling the system that all the commands you need are in that single directory. Since you also need standard system utilities like wc you also need to include the directories where they reside in your PATH. Normally you would do this to simply add the postgres directories to the list of directories searched for commands:
export PATH="/Library/PostgreSQL/11/bin:$PATH"

Create new local server in pgadmin?

I have PostgreSQL 11 and PGadmin 4 installed on windows. Currently I'm connected to a AWS server which hosts all of my data.
I want to create a local server (localhost) as a testing environment where I can experiment. I can't seem to do it though, and the other similar questions on stack don't help. Here's what my process is:
in pgAdmin, right click 'Servers' and go Create>Server
On the 'Create - Server' pop up box, i type in Name: Localserver. For 'connection' I type localhost. Port I leave as default '5432', db: postgres, username: postgres password: empty
click save.
however, I get an error:
Unable to connect to server:
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
What should I do? I am the admin if that makes a difference.
As a new Postgres user, I did not understand how to make use of Postgres on Ubuntu. So I'm just going to chime in and help out other newbies who perhaps cannot figure out how to work with Postgres on Linux. If you're using Windows, steps should be similar.
Before you get to using PgAdmin, a bit of configuration is required. Most will happen in your terminal at first.
Open a terminal using Ctrl + Alt + T if you're on a PC. Or just pres ALT + F1 and begin typing Terminal.
Let's start with the basics first and make sure you have proper
installation.
1. Installing Postgres Latest
1.1 update the system software packages
sudo apt update
1.2 install latest version of PostgreSQL from default Ubuntu repositories
sudo apt install postgresql
the installer will create a new PostgreSQL collection of databases
that will be managed by a single server instance
Default data directory : /var/lib/postgresql/your-version/main
Configurations files : /etc/postgresql/your-version/main
2. Checking if Postgres Service is Installed
2.1 Check if Postgres is Active
sudo systemctl is-active postgresql
You should see : active
2.2 Check if Postgres is enabled
sudo systemctl is-enabled postgresql
You should see : enabled
2.3 Check Postgres Service status
sudo systemctl status postgresql
You should see : active (exited) marked in green
2.4 Check if Postgres is ready to accept connections
sudo pg_isready
You should see : /var/run/postgresql:5432 - accepting connections
3. Configuring Postgres Authentication
3.1 Opening the pg_hba.conf as SUPERUSER
sudo code --user-data-dir=~/root /etc/postgresql/13/main/pg_hba.conf
I'm using visual studio code so for me code is vsc codename. If you're using vim or sublime just replace code with your text editor name.
3.2 Configuring pg_hba.conf
Notes: you shouldn't need to change anything here, just make sure your
configuration files matches the following lines :
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
Hit save and close.
3.3 Restart Postgres Service
sudo systemctl restart postgresql
4. Create NEW Server
For me, this is where all my confusion was. Before you use PgAdmin,
you need to create a server in your terminal, then you can connect and
manager it with PgAdmin just like you would with PhpMyAdmin. It's
actually easier.
4.1 Access the PostgreSQL database shell
sudo su - postgres
psql
You will then see this : postgres=#
4.2 Creating new server and user
postgres=# create user bob with superuser password 'admin';
That's how you create new user and server in Postgres. Let's move on to PgAdmin.
5. Installing pgAdmin4
5.1 Add public key for the repository
curl https://www.pgadmin.org/static/packages_pgadmin_org.pub | sudo apt-key add
Notes : if you don't have curl your Ubuntu will give you the command to install it
5.2 create the repository configuration file
sudo sh -c 'echo "deb https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/$(lsb_release -cs) pgadmin4 main" > /etc/apt/sources.list.d/pgadmin4.list && apt update'
5.3 install pgAdmin4
sudo apt update
sudo apt install pgadmin4
5.4 run the web setup script installed with the pgadmin4 binary package
sudo /usr/pgadmin4/bin/setup-web.sh
It will asks you for your email address and password. This email and
password is required to login to the PgAdmin4 web interface
6. Accessing pgAdmin4 Web Interface
6.1 Open your favorite browser
type in the address of the PgAdmin web interface. It usually looks like this
http://127.0.0.1/pgadmin4
Note : After installation the web setup script will tell you exactly
where to access the web interface.
When you see the login screen, enter the email and password you've chosen during the web setup script.
6.2 Adding New Server Connection
6.2.1 Click on Add New Server
6.2.2 Under General tab enter a name for your server. ANY name you would like, it doesn't matter. You could enter PgServer1 or whatever. Don't change anything else.
6.2.3 Switch to Connection Tab
Hostname/ Address : localhost
Port : 5432
Maintenance database : postgres (always)
Username : **bob** (the username youve chosen at 4.2)
Password : admin (or any password you chose at 4.2)
Hit Save.
Voila! You should be connected successfully. If not, just open terminal and create a new user like we did at 4.2
Notes : to create databases is very easy just right click on your
servername
create > databases
Useful Resources & Tutorials
PostgreSQL Fundamentals : Queries and the likes
PostgreSQL & JSON : useful for dealing with JS apps
PostgreSQL & Nodejs : Create realtime apps with nodejs and socket.io
More PostgreSQL Nodejs
https://github.com/supabase/realtime
https://whatsyourssn.com/posts/real-time-app-socket-postgresql/
UPDATE 2023
While following my own tutorial I ran into certificate issues at step 5 when I tried to create the repository file. The full error I was getting was this.
I want to post the solution that worked for me here.
This is probably one of two things.
You have set up a Postgres server, but have not adjusted listen_addresses in your postgresql.conf file, or
You have not set up a Postgres server on your local machine. ("Create Server" is a bit misleading, it should probably be "Create Server Connection".)
Usually a “connection refused” error indicates that the database server is either 1) not running, or 2) configured in such a way that it is not listening to the right port or IP address. Be sure to check ps -ef to see if Postgres is running, and also look at postgresql.conf to see if port and listen_addresses are set properly.
make sure the postgres service is running.
example in Linux : systemctl enable postgresql.service
Why? Attempting to connect to the server that is not running.
Action: Start your PostgreSQL server.
The path in the command below points to the data directory configured during the installation of PostgreSQL. Run this in Windows cmd.
pg_ctl -D "C:\user\PostgreSQL\data" start
>server started
After this go to pgAdmin and follow the initial steps as in question.
Create Server
Under General: Name: mytestServer
Under Connection: Hostname: localhost, Password: yourPassword,
keep other settings as default
Hit save
If you haven't, then first install the Postgres server on your machine. For windows, this is the currently active link for downloading the installation package: https://www.postgresql.org/download/windows/
As other answers here pointed out, PgAdmin is only a interface for using the Postgres server. Once you install the server locally, you'll be able to see the server on the left hand side pane after restarting PgAdmin.

Multiple installations of Postgres on Ubuntu

This is the first time I am setting up multiple Postgres postgres servers on Ubuntu 12.04LTS. (I know multiple versions is a bad idea, but need to sanity check new installation before I migrate and kill the old instance)
Original Postgres V9.1 is installed using bundled apt-get install. (Port : 5432)
New Postgres V10 is installed via EnterpriseDB package. (Port : 5433)
I am able to use standard utilities for the V9.1 like psql, pg_dump, etc. However, I am not able to access psql of V10. Instead I am getting this error message (note the incorrect port):
/opt/PostgreSQL/10/bin$ ./psqlpsql.bin: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
I would like to be able to manage both ideally by using psql.
I usually use Python to connect to databases and I am able to create an SQLAlchemy connection to new instance at port 5433. I can query pg_catalog and information_schema as well.
Thanks in advance!
The default port of postgres is 5432.
So If you want to access psql of V10, use the below commands:
export PATH=/opt/PostgreSQL/10/bin:$PATH
psql -p 5433

"psql: could not connect to server: Connection refused" Error when connecting to remote database

I am trying to connect to a Postgres database installed in a remote server using the following command:
psql -h host_ip -U db_username -d db_name
This is the error that occurs:
psql: could not connect to server: Connection refused
Is the server running on host "<host_ip>" and accepting
TCP/IP connections on port 5432?
Postgres installed version is 9.4.
Host operating system: Ubuntu 15.04
Client operating system: Centos 7
I already tried the following but the issue remains unresolved:
Edited pg_hba.conf file to include
host all all 0.0.0.0/0 md5
Edited 'postgresql.conf' and changed the listen parameter to
listen_addresses='*'
Restarted Postgres service.
Disabled firewall and iptables on host and client.
I checked by running the psql command locally and it worked.
I tried the second solution given in this question. Running nmap gave me the following output:
Starting Nmap 6.47 ( http://nmap.org ) at 2015-09-07 18:08 IST Nmap scan report for 10.17.250.250 Host is up (0.0000040s latency). Not shown: 997 closed ports PORT STATE SERVICE 22/tcp open ssh 25/tcp open smtp 80/tcp open http
Am I missing something? Hope someone can help.
cd /etc/postgresql/9.x/main/
open file named postgresql.conf
sudo vi postgresql.conf
add this line to that file
listen_addresses = '*'
then open file named pg_hba.conf
sudo vi pg_hba.conf
and add this line to that file
host all all 0.0.0.0/0 md5
It allows access to all databases for all users with an encrypted password
restart your server
sudo /etc/init.d/postgresql restart
Check the port defined in postgresql.conf. My installation of postgres 9.4 uses port 5433 instead of 5432
I have struggled with this when trying to remotely connect to a new PostgreSQL installation on my Raspberry Pi. Here's the full breakdown of what I did to resolve this issue:
First, open the PostgreSQL configuration file and make sure that the service is going to listen outside of localhost.
sudo [editor] /etc/postgresql/[version]/main/postgresql.conf
I used nano, but you can use the editor of your choice, and while I have version 9.1 installed, that directory will be for whichever version you have installed.
Search down to the section titled 'Connections and Authentication'. The first setting should be 'listen_addresses', and might look like this:
#listen_addresses = 'localhost' # what IP address(es) to listen on;
The comments to the right give good instructions on how to change this field, and using the suggested '*' for all will work well.
Please note that this field is commented out with #. Per the comments, it will default to 'localhost', so just changing the value to '*' isn't enough, you also need to uncomment the setting by removing the leading #.
It should now look like this:
listen_addresses = '*' # what IP address(es) to listen on;
You can also check the next setting, 'port', to make sure that you're connecting correctly. 5432 is the default, and is the port that psql will try to connect to if you don't specify one.
Save and close the file, then open the Client Authentication config file, which is in the same directory:
sudo [editor] /etc/postgresql/[version]/main/pg_hba.conf
I recommend reading the file if you want to restrict access, but for basic open connections you'll jump to the bottom of the file and add a line like this:
host all all all md5
You can press tab instead of space to line the fields up with the existing columns if you like.
Personally, I instead added a row that looked like this:
host [database_name] pi 192.168.1.0/24 md5
This restricts the connection to just the one user and just the one database on the local area network subnet.
Once you've saved changes to the file you will need to restart the service to implement the changes.
sudo service postgresql restart
Now you can check to make sure that the service is openly listening on the correct port by using the following command:
sudo netstat -ltpn
If you don't run it as elevated (using sudo) it doesn't tell you the names of the processes listening on those ports.
One of the processes should be Postgres, and the Local Address should be open (0.0.0.0) and not restricted to local traffic only (127.0.0.1). If it isn't open, then you'll need to double check your config files and restart the service. You can again confirm that the service is listening on the correct port (default is 5432, but your configuration could be different).
Finally you'll be able to successfully connect from a remote computer using the command:
psql -h [server ip address] -p [port number, optional if 5432] -U [postgres user name] [database name]
Make sure the settings are applied correctly in the config file.
vim /etc/postgresql/x.x/main/postgresql.conf
Try the following to see the logs and find your problem.
tail /var/log/postgresql/postgresql-x.x-main.log
Following configuration, you need to set:
To open the port 5432 edit your /etc/postgresql/9.1/main/postgresql.conf and change
# Connection Settings -
listen_addresses = '*' # what IP address(es) to listen on;
In /etc/postgresql/10/main/pg_hba.conf
# IPv4 local connections:
host all all 0.0.0.0/0 md5
Now restart your DBMS
sudo service postgresql restart
Now you can connect with
psql -h hostname(IP) -p port -U username -d database
Step 1: edit file potgresql.conf
file location should be : etc/postgresql/10/main/
Look for:
#Connection Settings -
#listen_addresses = '' # what IP address(es) to listen on;
remove # before listening addresses
add '*' :
listen_addresses = '*'
Step 2: edit file pg_hba.conf
file location should be : etc/postgresql/10/main/
add below given line at the end
host all all 0.0.0.0/0 md5
Step 3: restart postgres server
sudo /etc/init.d/postgresql restart
Step 4: check postgres server status
sudo netstat -plunt |grep postgres
Make sure you are using same port to access the DB
Mine was quite straightforward if you are on a Mac try:
brew install postgres
This will tell you if you have it already install and what version or install the latest version for you if not then run
brew upgrade postgresql
This will make sure you have the latest version installed then finally
brew services start postgresql
This will start the service again. I hope this helps someone.
I think you are using the machine-name instead of the ip of the host.
I got the same error when i tried with machine's name. Because, It is allowed only when both the client and host are under same network and they have the same Operating system installed.
In my case, I did not change azure default security policy in management portal. The original is port 22 allowed and the rest are all denied. As long as I add 5432 port, everything becomes good.
The following helped me on macos Mojave:
$sudo mv /usr/local/var/postgres /usr/local/var/postgres.save
$brew uninstall postgres
$brew install postgres
See the port and make a port change in postgresql.conf. My installation of postgres 9.4 uses port 5431 or 5434 instead of 5432.
If it say the port is in use so change the port.
And check if you give password in psql installation so give the password in file and save it.
In my case I had removed a locale and generated another locale. Database failed to open because of fatal errors in the postgresql.conf file, on 'lc_messages', 'lc_monetary', 'lc_numberic', and 'lc_time'.
Restoring the locale sorted it out for me.
Another situation,postgresql.confandpg_hba.conffile not locate at /etc/postgresql/9.1/main/.Because postgres can start at any location you set.
For example when you use command pg_ctl -D /tmp/pgsql/ start ,the postgresql.conf and pg_hba.conf will located at /tmp/pgsql/.
I had the exact same problem, with my configuration files correct. In my case the issue comes from the Eduroam wifi I used : when I connect via another wifi everything works. It seems that Eduroam blocks port 5432, at least in my university.
Try to migrate your database. For instance, if you are using Heroku to host your project and with Django, then try heroku run python manage.py migrate command; the error should go away.
I had a problem like this where I had to ssh into a server and than run a query in psql console so the query was in a script but everytime I got this error psql not found so what I did was just added the psql full path from the bin which we get from cat .bash_profile and its done
PATH=$PATH:/usr/local/pgsql/bin:/usr/local/mysql/bin
So I added the whole /usr/local/mysql/bin/psql intead of just psql for remote execution.
and another one here:
both host and remote are on real servers
you need '*' exactly.
'localhost , xxx.xxx.xxx.xxx' will not work. all these answers(i've seen two) should be wiped out.
what you don't need : host all all 0.0.0.0/0 md5 and this stuff
For me, I just removed the existing PostgreSQL 14 Server which was on the left-hand side of the pgAdmin4 GUI interface under the servers and then I manually added a new server from the option which is inside Quick Links of pgAdmin4.
I followed the documentation of bitnami.com.
I had a problem with access to external server via 5432.
I noticed that any network but mine saw the service
nmap server -p 5432
Fortunatelly, I recalled that I was playing with exposing my internal postgres server to outside world using my mikrotik router.
Somehow it effectively closed external 5432 for internal network.
As soon as I removed all nat rules with 5432 port - it worked like a charm.