How to check table value and export the database using PostgreSQL and Ubuntu - postgresql

I need one help. I need to export one database from my postgreSQL which is running in my ubuntu server.
first i typed the below command
sudo -u postgres psql postgres
it gave me the following result.
I checked all database using below command
\list
It gave me the following result.
I needed to connect the 100salons_production database and i connected using the following command and checked the tables.
\connect 100salons_production
\dt
it gave the following result.
Now i tried to check the table values using this command select * from areas but it does not show any result.
Here I need to check the tables results.and also i need commands to export this database and import it in somewhere else. Please help me to resolve this issue.

If you only want to export the database, there is not need to connect to it using the psql tool. Instead, you have to use the pg_dump tool. You can do that like this:
sudo -u postgres pg_dump DATABASE_NAME > dump.sql
Then you should transfer dump.sql to the other computer and import it there.

Related

Problem restoring databse between 2 RDS instances using pg_dump and pg_restore

I'm having difficulty restoring a DB to an AWS RDS Postgresql instance. Context is that i am backing up from one RDS instance and restoring to another RDS insurance. They both have the same version of Postgresql 9.6.5.
I was able to take a dump using the following command:
./pg_dump.exe -U dbuser -W -h prod-pgsql-rds.3ft5coqxjdnq.eu-west-2.rds.amazonaws.com -d devdb > c:\tmp\backup.sql
From the resulting .sql file, I then attempted a restore to another RDS instance which is also using Postgresql 9.6.5 using below command:
./pg_restore.exe -U dbuser -d testdevdb -h dev-pgsql-rds.cym8coqx52lq.eu-west-2.rds.amazonaws.com "c:\tmp\backup.sql"
*I also tried the -f switch in the above restore command instead of the " " quotes before/after the file name
But when I try to restore it to a newly created database I get the following error:
pg_restore: [archiver] input file does not appear to be a valid archive
Can anyone help? FYI, I am using PGAdmin 4 via Windows PowerShell. I have to edit some of the values in the strings above due to data sensitivity.
pg_restore is only used for the other, non-plain-text output formats that pg_dump can output. For .sql dumps, you just use psql. See the docs on restoring from backups.
In a Unix env, you'd do psql [yourflags] < /tmp/backup.sql, but I'm unfamiliar with powershell and don't know if it supports < for input redirection; hopefully either it's present or you know the equivalent PowerShell syntax.
So I couldn't get psql or pg_restore to work so opted to import the .SQL file into via the SQL query tool in PGAmdin. This through up some errors so had to make several changes to the .SQL file and perform below:
Commented out a couple of lines that were causing errors
Elevated permissions for the user and made him the owner of for the Schema and DB properties by right-clicking on these via PGAdmin
The .sql file was making several references to the user from the source RDS DB so had to do a find and replace with a user account created for the destination RDS DB. Alternatively, I could have just created a new user on the destination DB with the same username and password as the source DB and then make him the owner in ref to step 2.

Creating a database dump doesn't seem to be working

I have PostgresQL 9.6.2 installed, and I'm trying to make a backup of one of my databases (my_database) using pg_dump command. It doesn't seem to be working as I see no output file.
I'm using a Mac and from my terminal (and in my project directory) I use:
psql postgres postgres
pg_dump my_database > my_database.bak
being postgres my database default user and password.
I've already tried using sudo psql postgres postgres with the same results.
I imagine that I'm experiencing some kind of lack of permissions but cannot understand it.
So, how to have the right permissions to do this?
Postgres comes with a bunch of stuff, including command-line tools. That includes the standard client, psql, and also the command pg_dump. That needs to be invoked from a shell command line.
If you type it into a psql command line, you'll get a syntax error once you terminate the line (with a ';') -- it's not valid sql, nor a valid command to psql.
Hope that helps!

How do I export some data my local Postgres db and import it to a remote without deleting all the data from the remote?

I’m using Postgres 9.5 on Mac Sierra. I want to export some table data from my local machine and import that into a Postgres 9.5 database on a Linux machine. Note I don’t want to blow away the data on the Linux machine, only add the my local machine table rows to the rows that already exist on the tables in the Linux environment (and ignore duplicates). So on my local machine, I ran
pg_dump -U myusername --format custom --section data --inserts --file "t1.backup" --table "table1" --table "table2" --table "addresses" "mydb"
However on my remote machine, when I try and import the file, I get the error
myuser#remote-machine:/tmp$ psql db_production < t1.backup
The input is a PostgreSQL custom-format dump.
Use the pg_restore command-line client to restore this dump to a database.
But I don’t want to use pg_restore because I don’t want to erase the existing table data, I simply want to add to it. How can I achieve this?
Use --format plain instead of custom one. The latter is designed to work exclusively with pg_restore. Plain format also allows you to take a look into dumped data with text editor and verify if that's what you want.
However my quick test shows that it's also possible to append data with pg_restore and custom format data-only dump:
pg_restore -d db_production --data-only t1.backup

psql: FATAL: database "Morne Nel" does not exist

I need to do a report in JasperReports.... besides the point here.
Ive been given a existing Postgresql DB to use.
I have installed PostgreSQL and all went great. (except the package update at the end. Cant get past the proxy server)
Ive added the C:\Program Files\PostgreSQL\9.0\bin path the the system path as-well.
When I go to cmd and type psql it propts for a password....
I enter password, because thats the only password I added during instalation.
Then is comes up with psql: FATAL: database "Morne Nel" does not exist
step by step hoe do I import this DB into postgres? PLEASE?
When using psql you have to pass database name, otherwise postgres will try to connect to database matching your user name (that is why it tries to connect to "Morne Nel").
You can use createdb console command to create database first. Here is documentation on that command: http://www.postgresql.org/docs/9.3/static/sql-createdatabase.html
But also you can use pg_restore command right away with --create option passed to create and restore database from dump at once (documentation here: http://www.postgresql.org/docs/9.3/static/app-pgrestore.html).
Try to execute those commands (it is possible that you will have to specify your postgres user and password) and let us know what happened. I cannot write more at the moment as I am not sure what dump format do you have so it would be good to provide that information if you won't manage to use pg_restore successfully.

How to copy table from one database to another using postgresql

I am trying to copy a table from one database to another database But no response in server
Here is the command
pg_dump -U database_user_name -t categories my_current_db_name | psql new_db_name
What wrong in the above code it is not showing any response.
If something does not work then divide it into smaller problems.
At first try to run:
pg_dump -U database_user_name -t categories my_current_db_name
What is it printing? Do you see error message or correct dump output?
Then try to run:
psql new_db_name
Is it able to connect to database?
Possible problems:
new_db_name does not exists
you cannot connect with source or destination database because of wrong user/password/db host/port