Creating a copy of the database in PostgreSQL - postgresql

I'm trying to create the first copy of my database. I'm using PostgreSQL and Ubuntu 16+ with Django technology.
I found this documentation to create a copy:
I'm trying to export the entire database to a file so that I can add it to another server. I tried this:
pg_dump app_prod > test_copy
pg_dump --host=localhost --username=app --dbname=app_prod --file=testdb.sql
after selecting ls my directory can see the database. But by running eg WinSCP it is not visible.
How can I take these files, copy them to my Windows system and upload to another Ubuntu server?
I think that it is enough to make them visible in WinSCP. How can I do this?
EDIT:
drwxr-xr-x 3 postgres postgres 4096 Oct 4 08:06 9.5
-rw-rw-r-- 1 postgres postgres 3578964 Jan 18 10:46 test_copy
-rw-rw-r-- 1 postgres postgres 0 Jan 18 10:54 testdb.sql

It seems like this was resolved in the comments: you were looking at the wrong folder in the WinSCP folder explorer.
There are a few items worth noting to bolster the good advice already given:
Your ls -l output indicates that the SQL file is zero bytes in size, so something has gone wrong there. If you manage to transfer it to your local machine, you will find it is empty.
Also, try not to store database dumps in /var/lib/postgresql - this is where your PostgreSQL database keeps live database files on the server, and you don't want to risk changing or deleting anything here. Use /home/maddie instead (change the username as appropriate).

Related

Postgres COPY TO not working for intended destination directory

I have written a function that prepares data based on user-input from the web and copies the results down as a csv to a specified directory, from where the package (each request will contain information from 3 different tables/csv files) will be made available for the user to download.
The function itself works if the output directory is in the pg_data directory /var/lib/pgsql/data/adc_downloads, the permissions are:
drwxr-xr-x 2 postgres postgres 4096 Dec 5 15:18 adc_downloads
But ideally I want the output directory to be here: /var/www/vhosts/[vhostname]/httpdocs/adc_downloads. After getting it to work in pg_data, I used chown -R --reference=/var/lib/pgsql/data/adc_downloads /var/www/vhosts/[vhostname]/httpdocs/adc_downloads and chmod with the same parameters to mimic the permissions which appears to have worked:
drwxr-xr-x 2 postgres postgres 4096 Dec 5 14:19 adc_downloads
but I'm still getting a Permission denied error when trying to run the function with the intended output directory.
Something else then must be overriding the permissions I've (tried to?) set, any ideas what this might be? Given the function itself works in one directory but not another I don't think it's an SELinux issue, though I may of course be wrong.
Version Info: Postgres 8.4 CentOS 6.5 built with Plesk.
got this working (thanks to #joop's recommendation) in the intended location /var/www/vhosts/[vhostname]/httpdocs/adc_downloads by adding execute permission for the other users (including the postgres user) for the [vhostname] and httpdocs directories

How to connect to postgresql from files?

Ok, so I have a client who has a postgresql database. He sent me the files (they look like this: http://prntscr.com/fbyz2l).
I have PGAdmin 4 on my windows 10 box. I also have postgres installed locally.
I have the database name and login information... but I can't figure out how to connect to the database.
I am guessing it is pretty simple, but I am having a tough time googling the right thing to get some help.
Update
I am still hunting this, but my feeling is the files are not the right format to import or bring onto my localhost. I am asking for a backup file that PGAdmin can make. If anyone has input on this, I am all ears.
Update 2
So I copied all the files to C:\Program Files\PostgreSQL\data\pg96\base
Restarted the server. When I do a
psql -h "C:\Program Files\PostgreSQL\data\pg96\base" -l
I get this:
http://prntscr.com/fbzxf2
I can connect to template1 and postgres, but neither of them is my database (184429). Ugh...
Thanks!
These files are basically copy of the postgres DB . You need to restore it on your machine .
You can find default path of postgres DB on C:\Program Files\PostgreSQL\some version\data location . Place the root of these files within that directory and restart your postgres service .
After that you can find new database in your PGAdmin .

Moved postgres data file and postgres started writing to a new file with same name

I have a directory where postgres was writing to a file: 15426233.4
-rw------- 1 postgres sql 1.0G Feb 4 13:41 15426233
-rw------- 1 postgres sql 149M Feb 4 13:41 15426233.4
-rw------- 1 postgres sql 1.0G Feb 4 13:41 15426233.3
drwx------ 3 postgres sql 75K Feb 4 13:40 .
-rw------- 1 postgres sql 1.0G Feb 4 13:34 15426233.2
-rw------- 1 postgres sql 1.0G Feb 4 13:28 15426233.1
Initially this file 15426233.4 was under /data5/PG_9.1/15411/15426233.4. Due to disk space getting filled up under /data6 partition I ended up moving to /data8 however normally we run a symlink so that /data5/PG_9.1/15411/15426233.4 would now symlink to /data8/PG_9.1/15411/15426233.4. Due to lack of disk space the symlink creation failed but move still happened. A while later postgres started writing to a new file /data5/PG_9.1/15411/15426233.4. Is there a way I can stop the db, consolidate the data in /data5/PG_9.1/15411/15426233.4 to /data8/PG_9.1/15411/15426233.4, then create a symlink /data5/PG_9.1/15411/15426233.4 that points to /data8/PG_9.1/15411/15426233.4 and restart the postgres db?
Never, ever, mess with individual files in the data directory. And never ever change catalog tables (pg_tablespace) manually. Everything you need to do can be done through SQL or by handling the complete data directory.
You have several ways to move data to a bigger disk:
Tablespaces
When you create a new tablespace you can put that on the other disk. Then move the tables in question to the new tablespace:
create tablespace bigdata location '/path/to/bigdisk';
Then move the tables to the tablespace:
alter table bigtable set tablespace bigdata;
There is no need to mess around with the data directory manually.
Move the data directory
Caution!
Only do this after stopping Postgres!
Once you have stopped the Postgres server, copy the complete(!) existing data directory to the new disk (make sure you have Postgres stopped before you do that). It's safe to copy the data and later delete the original if everything is OK rather than moving it right away.
After copying the data, adjust postgresql.conf to point it to the new data directory: http://www.postgresql.org/docs/current/static/runtime-config-file-locations.html
Rename the old data to make sure you will see an error if the configuration wasn't changed properly.
Start Postgres again.
The physical layout of the disk storage is documented in the manual:
http://www.postgresql.org/docs/current/static/storage.html

Importing large data to postgresql

I recently dual booted my system by installing Ubuntu over Windows. Now I have to import a file in postgresql , which is stored in host file system. The host filesystem has 190 GB of space. But when I log into postgres as sudo su postgres, it would take me into root filesystem(the default postgres folder) and query would be executed in that. Now my data set is of 3 GB and after sometime query would return 'OUT of disk space' as the root filesystem is of 3.5-4 GB. So it would be great if anyone can suggest solution to this? . Do I need to change default folder of postgres?
Thanks
Ravinder
I'd create a new file on host, which I'd configure as a second hard drive image for your Ubuntu. And I'd use this drive to create partition there and mount it where the PGDATA directory would be.

How to manage the wals dir on a PostgreSQL replication server?

I have two PostgreSQL 9 servers running on Amazon EC2. One is the master, the other is a replication server in standby.
The replication server is continuing to fail as the hard drive fills up. It appears that the following directory is constantly growing:
/usr/local/pgsql/wals
There are thousands of files like:
-rw------- 1 pgsql users 16777216 Jan 3 20:36 000000010000001B000000A2
-rw------- 1 pgsql users 16777216 Jan 3 20:40 000000010000001B000000A3
-rw------- 1 pgsql users 16777216 Jan 3 20:46 000000010000001B000000A4
How do you set this up to not cause failure? Do you need to auto-rotate the wal files? Or? Could really use your advice. Thank you
You should configure an archive_cleanup_command in your recovery.conf file. Check out pg_archivecleanup; it's made for this purpose.
The WAL files could also serve as an archive for backup and recovery purposes, so they are not automatically deleted if you are just doing replication.
(Alternatively, you could use whatever hand-crafted method you like to clean up the archive, but that could be a bit difficult and error prone.)
There is a setting in the postgresql.conf file called wal_keep_segments= and checkpoint_segments= I have mine set to wal_keep_segments=128 and checkpoint_segments=128 with replication fine.
wal_keep_segments are the minimum wal_files in the directory.
checkpoint_segments are the maximum.
I set it on both.