Cannot migrate postgres db between 2 servers ubuntu - postgresql

I'm trying to migrate data from old server to new server, both of them used ubuntu server.
I have used this command:
pg_dump -C -h localhost -U localuser dbname | psql -h remotehost -U remoteuser dbname
but it throw:
psql: could not connect to server: Connection timed out
Is the server running on host "X.X.X.X" and accepting
TCP/IP connections on port 5432?
I have checked postgresql conf on new server, running on 5432, listen..="*"
Anything wrong?
Thanks all !

Related

Postgres connection issue

I installed and started my Postgres database with brew (on my Mac). I also defined an entry in my /etc/hosts file (I tried both with 127.0.0.1 postgres and with postgres).
However, when I try
psql -h postgres -U postgres -p 5432
I cannot connect
psql: could not connect to server: Connection refused.
However, when I try with
psql -h localhost -U postgres -p 5432
I can connect. What is needed to be able to connect with: psql -h postgres -U postgres -p 5432
Make sure your PostgreSQL server is willing to accept tcp/ip connections on port 5432.
In your PostgreSQL configuration file check these values.
listen_addresses = '*'
port = 5432

PSQL: SSL error: unknown protocol

all. I have a new Ubuntu 17.04 server setup. I can access it through ssh successfully. Also, I have a postgresql database installed. It is already configured to accept remote connections through the following configurations:
pg_hba.conf
host all all 0.0.0.0/0 md5
postgresql.conf
listen_addresses = '*'
But when I try to access the database from my computer, I get the following error:
psql -h [SERVER_IP] -d db_production -U dbuser
psql: SSL error: unknown protocol
expected authentication request from server, but received S
I also have a docker container running inside the server. If I access it through /bin/bash and also try to connect with the server db, I get this error:
psql -h [SERVER_IP] -d db_production -U dbuser
psql: could not connect to server: Connection refused
Is the server running on host "[SERVER_IP]" and accepting
TCP/IP connections on port 5432?
The port 5432 is opened and the firewall is disabled. Does anybody have gone through that?
First, do you use the local server host when you're connected into the docker? The server IP, in that case, should be localhost or 127.0.0.1.
Try to use the complete DB URL:
psql postgres://dbuser:dbPass#SERVER_IP:5432/dbname?sslmode=prefer

pgAdmin3 backup over ssh tunnel

I have a running postgresql server on amazone ec2. I connect with pgAdmin3 to it over ssh tunnel directly configured in pgAdmin3 from my mac.
I can make queries and see the full schema, no problem about that.
If I try to make a backup of the database (from pgAdmin3 GUI) then I get (even if the connection is actually open and working) the following exception:
/Applications/pgAdmin3.app/Contents/SharedSupport/pg_dump --host localhost --port 5432 --username "MY_USERNAME" --role "MY_ROLE" --no-password --format custom --encoding UTF8 --verbose --file "/Users/XXX/filename" "DATABASENAME"
pg_dump: [archiver (db)] connection to database "DATABASENAME" failed: could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
Process ended with Exitcode 1.
Any idea why pg_dump in the background can not connect over the ssh tunnel?
alternative until i found a solution is to make it by terminal
ssh <HOST> "pg_dump -U <USERNAME> -W -h localhost -F c <DATABASENAME> | gzip -c" > ./backup.sql.gz
This line worked for me:
ssh -o "Compression=no" server_adress "pg_dump -Z9 -Fc -U postgres db_name" > backup_name.dump

pg_dump from remote server to localhost

Hi can anyone help me dump from a postgreSQL database on a remote AWS server to a postgreSQL database on my local machine.
I've been trying to do it using the answer in this stack post but it keeps failing.
The command I'm using is
pg_dump -C -h ssh ubuntu#ec2-59-16-143-85.eu-west-1.compute.amazonaws.com -U dev_user paycloud_dev | psql -h localhost -U dev_user paycloud_dev
But I keep getting the error
pg_dump: too many command-line arguments (first is "paycloud_dev")
Can't figure out what I'm doing wrong
Just to add, dev_user is the role I've set up in postgreSQL on both the local machine and remote server. paycloud_dev is the name of the database on both (owner is dev_user)
Edit 1
Tried the command below as per a post that has since been deleted for some reason
pg_dump -C -h ec2-59-16-143-85.eu-west-1.compute.amazonaws.com -U dev_user paycloud_dev | psql -h localhost -U dev_user paycloud_dev
This is now giving me the error
pg_dump: [archiver (db)] connection to database "paycloud_dev" failed: could not connect to server: Connection refused
Is the server running on host "ec2-59-16-143-85.eu-west-1.compute.amazonaws.com" (59.16.143.85) and accepting
TCP/IP connections on port 5432?
I went on to AWS and noted that is the elastic ip of the server. I then tried the following (the private IP address)
pg_dump -C -h 170.30.43.35 -U dev_user paycloud_dev | psql -h localhost -U dev_user paycloud_dev
This asks me for the password for paycloud_dev and when I enter it pauses for a good 2 or 3 minutes and comes back with:
pg_dump: [archiver (db)] connection to database "paycloud_dev" failed: could not connect to server: Connection refused
Is the server running on host "170.30.43.35" and accepting
TCP/IP connections on port 5432?
I've tried editing the AWS security group to add a rule that accepts all traffic (port range 0-65535) but the same error is occurring.
Edit 2
Tried the following as per post by pokoli
ssh ubuntu#ec2-59-16-143-85.eu-west-1.compute.amazonaws.com pg_dump -C -h -U dev_user paycloud_dev | psql -U dev_user paycloud_dev
It's not working though. It first asks me for the psql password for my laptop then before I can input anything, it gives an error.
[sudo] password for alzer: pg_dump: too many command-line arguments (first is "paycloud_dev")
Try "pg_dump --help" for more information.
Anyone?
You have to connect with ssh to remote host, execute the dump and pipe it to your local machine. The following command should do:
ssh ubuntu#ec2-59-16-143-85.eu-west-1.compute.amazonaws.com -C pg_dump -U dev_user paycloud_dev | psql -U dev_user paycloud_dev
The command will ask for password of both users if needed and the playcloud_dev database should exists on localhost, otherwise the dump will fail.
Try do this over ssh tunnel.
ssh -fT -L 5432:127.0.0.1:5432 %remote_user_login%#%your_aws_host% sleep 10
pg_dump -C -h localhost -U dev_user paycloud_dev | psql -h localhost -U dev_user paycloud_dev
First line create ssh tunnel and port mapping.
And view your AWS security settings in "Security Groups", may be you forgot opening ports.

Connection refused while creating connection pool for postgresql vendor in glassfish server admin console

I am trying to create a connection pool to a PostgreSQL database in Glassfish server.
What I have done the steps as:
I copied the jar of postgresql jdbc jar in full path
C:\Program Files\glassfish-3.1.1\glassfish\domains\domain1\lib\ext
I have specified full path to the jar in the glassfish server admin console:
Pool Name: post-gre-sql_CommonPush_postgresPool
Resource Type: javax.sql.XADataSource
Datasource Classname: org.postgresql.ds.PGSimpleDataSource
In case of additional properties I have specified as:
driverClass:org.postgresql.Driver
URL: jdbc:postgresql://10.137.243.1:5432/CommonPush
portNumber: 5432
databaseName: CommonPush
serverName: 10.137.243.1(My system's IP)
user: postgres
password:
When I ping the above settings I get the error as:
An error has occurred
Ping Connection Pool for post-gre-sql_CommonPush_postgresPool is
Failed. Ping failed Exception - Connection could not be allocated
because: Connection refused. Check that the hostname and port are
correct and that the postmaster is accepting TCP/IP connections.
Please check the server.log for more details.
Ping failed Exception - Connection could not be allocated because:
Connection refused. Check that the hostname and port are correct and
that the postmaster is accepting TCP/IP connections. Please check the
server.log for more details.
I am not able to figure out this problem, can anyone help?
You should use the Resource Type javax.sql.DataSource. I'm not sure if this is causing the error, you may also try to change the hostname to localhost.
Can you access the PostgreSQL DB via pgAdmin or psql commands?
Cause could be that the user may not be having enough privileges on the db objects.
Grant the required privileges to the user, like:
###GRANT USAGE ON service_schema TO admin
sudo -u postgres psql -p 5432 -d service_db -c "GRANT USAGE ON SCHEMA "service_schema" TO "admin";"
###GRANT PREVILEGES ON TABLES OF service_schema TO admin
sudo -u postgres psql -p 5432 -d service_db -c "GRANT ALL ON TABLES IN SCHEMA "service_schema" TO "admin";"
###GRANT PREVILEGES ON SEQUENCES OF service_schema TO admin
sudo -u postgres psql -p 5432 -d service_db -c "GRANT ALL ON SEQUENCES IN SCHEMA "service_schema" TO "admin";"
###GRANT PREVILEGES ON FUNCTIONS OF service_schema TO admin
sudo -u postgres psql -p 5432 -d service_db -c "GRANT ALL ON FUNCTIONS IN SCHEMA "service_schema" TO "admin";"