initlocation not working in postgresql - database

I am using postgresql 9.2 on a RHEL 6.5 machine and would like to set up a database in a non-standard location as it is filling up the root partition. The instructions tell me to use the initlocation command from the command line but I get the error 'command not found' on using it. I have searched for it in case it is a shell script but cannot find it anywhere.
How do I run this command? thanks for any help.

Right, so you want to leave the existing installation where it is and create a new database in a directory that has more space.
First, a disclaimer, I haven't done exactly this before, but, I have a 9.4b2 installation here on my home computer and I did walk through these steps to verify that is seems to work :-)
#Mike Sherrill 'Cat Recall' has a pretty good write up on database storage (Where does PostgreSQL store the database?). I don't think it specifically answers your question, but it does have all of the information necessary to figure it out.
In your case, you will want to use tablespaces. First, create a new tablespace in the postgres installation. I will use my machine as an example. The first thing I would do is identify where the new database will reside. A directory name, and it has to be empty. For me, I'll create an empty directory in my home directory (I have to do it as the root user) with the shell commands:
mkdir /home/gfausak/pg_tablespace
chown postgres /home/gfausak/pg_tablespace
chgrp postgres /home/gfausak/pg_tablespace
chmod 700 /home/gfausak/pg_tablespace
Then I log in to psql as the postgres user. Like:
psql -Upostgres
Your incantation might be different. When you get to the prompt just verify where your current database(s) are via:
postgres=# show data_directory;
data_directory
-----------------------
/var/local/pgsql/data
(1 row)
Now, create your new tablespace.
postgres=# create tablespace newspace location '/home/gfausak/pg_tablespace';
CREATE TABLESPACE
The docs for this command can be views here: http://www.postgresql.org/docs/9.2/static/manage-ag-tablespaces.html
Once created it can be used for a variety of different objects. In your case, createdb, can be done from the command line or from the postgres psql prompt. I'll create a test database in that tablespace:
postgres=# create database newdb with tablespace = newspace;
CREATE DATABASE
Just for grins, open it and create a table:
postgres=# \c newdb
You are now connected to database "newdb" as user "postgres".
newdb=# create table testme (t text);
CREATE TABLE
newdb=#
If I go back to my home directory and take a peek in that newly created directory I'll see some postgres files:
ls /home/gfausak/pg_tablespace
PG_9.4_201407151
Anything you create in the newdb database will go in to the new directory unless you override the creation of it. There is a default_tablespace variable. As long as it is blank, the default is to put objects in the current database. Same goes for another setting temp_tablespace. The default for both of these is blank, meaning the current database.
You can even create objects in your old database in the new tablespace by specifying the tablespace when you create the table.
-g

Try this.
vi /etc/init.d/postgresql-XX
Change this variable with:
PGDATA=/var/lib/pgsql/$PGMAJORVERSION/data
PGLOG=/var/lib/pgsql/$PGMAJORVERSION/pgstartup.log
on:
PGDATA=/new_pgdata/pgsql/${PGMAJORVERSION}/data
PGLOG=/new_pgdata/pgsql/${PGMAJORVERSION}/pgstartup.log
than:
mkdir -p /new_pgdata/pgsql/XX/data/
chown -R postgres:postgres /pgdata/pgsql
/etc/init.d/postgresql-XX initdb -D /new_pgdata/pgsql/XX/data/
Where XX is equal to 9.2

Related

Do not drop and create database when scripting drop/create database objects and data with mssql-scripter

I'm trying to script the database objects and data of my database to later move it to a server where I don't have backup/restore rights. Instead I'm using the Generate Scripts method and I use mssql-scripter to generate the scripts.
I have a .bat file with the following script code to generate my SQL script file.
set
timevar=%date:~4,2%%date:~7,2%%date:~10,4%-%time:~0,2%%time:~3,2%%time:~6,2%
mssql-scripter --server 10.100.8.8 -d Dev_db -f .\%timevar%.sql
--schema-and-data --script-drop-create --target-server-version 2016 --target-server-edition Standard --check-for-existence --include-dependencies --constraint-names --collation -U ScriptingUser -P 1234 --exclude-use-database
The problem is that it's also scripting DROP DATABASE and CREATE DATABASE, which I don't want. I would only like to DROP and CREATE database objects and later populate tables with the scripted data.
Has anyone faced this problem and have you found a solution?
After fiddling around with the options for longer, I managed to find the right parameter and work-around to solve my problem.
The exact code that I ran is:
set
timevar=%date:~4,2%%date:~7,2%%date:~10,4%-%time:~0,2%%time:~3,2%%time:~6,2%
mssql-scripter --server 10.100.8.8 -d Dev_db -f .\%timevar%.sql --schema-and-data
--script-drop-create --target-server-version 2016 --target-server-edition Standard --check-for-existence --constraint-names --collation -U ScriptingUser -P 1234 --exclude-use-database --include-objects "dbo." --display-progress
The key change I added the --include-objects parameter, with a twist. The way I changed by scripts is by adding code snippet:
--include-objects "dbo."
This tells mssql-scripter to only script out objects that contain the "dbo." keyword(substring) in the fully qualified name.
Also I remove this parameter from my initial command:
--include-dependencies
since I script out everything in my database under the dbo schema.
This scripts out:
all of the objects in my database
it includes a IF EXISTS check
it issues a DROP query to drop the existing
it issues a CREATE query to create the new one
it issues multiple INSERT statements to also populate the database with data

Synchronize database files with the database - PostgreSQL

First forgive me for my English. It is a little bad. Second forgive my ignorance, i'm newiest in postgres
I'm having trouble when I try to up a backup database on another database. I need to dump the database just to get one table, but I only have the files that was in /var/lib/pgsql/data/base/
Here what I try:
I create a database named "test" with OID 227763 so I put the files of the old database to this new database with another OID. I fix the folder and files permissions, but when I log into "test" and run select * from pg_tables; the tables does not appears to me. And when I try to create the table on PhpPgAdmin, I got
ERROR: relation already exists
I'm trying to do this because I need to know which of this files is the table that i want. I will log into database and run SELECT oid,* from pg_class; to get the OID.
I found the old OID database in /var/lib/pgsql/data/global/pg_database
If anyone can help me, I thank you.
There are many ways to backup and restore an entire database or a single table. It sounds like you need to be using pgDump instead of working on individual files. A file level copy is likely to corrupt your database if not in backup mode and if not copying the entire thing + archive logs.
If you MUST copy it by files, make sure the database is shut down for maximum safety.
For me, if I had one table to backup, I'd use pg_dump
pg_dump -U {user-name} {source_db} -f {dumpfilename.sql}
you can use the -t flag to list a single table if you like.

Postgres backup not copying contents

I am working on PostgreSQL database and we have a test server which needs to have the same data set as the production one. For this, I plan to start a daily CRON job in linux and copy the production database along with its contents like tables, rows, columns, sequences.
I checked how to copy databases from one to another, and I used the pg_dump command as I will write it below, but it only copied the database tables, sequences, but not the contents.
What should I do to copy the contents?
pg_dump -C databaseName | ssh -C username#removeHost.com "psql databaseName"
Edit
So, What I did was I deleted the database which was on test server,
created a new empty database and then used the command above, and it
worked. So I guess I need to delete the database then only it will
overwrite it.
What should I do to circumvent this behaviour and do a force update
of the database, or delete the test server database even if it is use
and create a new empty database.
Have you tried to use pg_restore instead of psql ? pg_restore has special arguments for your case: -c -C.
Details here:http://www.postgresql.org/docs/current/static/app-pgrestore.html
An example of a command to dump/transfer/restore a db:
pg_dump -F c databaseName | ssh -C username#removeHost.com 'pg_restore --clean --create -d postgres'
For this command you need an empty db on target instance to connect to. (postgres in example).
database named with -d is used only to issue the initial DROP DATABASE
and CREATE DATABASE commands. All data is restored into the database
name that appears in the archive.
If you already have a db on target instance:
pg_dump -F c databaseName | ssh -C username#removeHost.com 'pg_restore --clean -d databaseName'
Similar question: Use pg_dump result as input for pg_restore

improperly importing sql file in postgres cli

I have a question about importing a sql file to postgres CLI. I may have been improperly importing my file or either I may have some User or Database privilege?!? issue. Anyways, these are just my hunches. I am trying to pinpoint the cause of this message after importing a sql file.
The message that I get is:
No relations found.
The steps I did to get into Postgres are:
I typed in:
sudo -i -u postgres
psql
then i created a new role, altered the role permission
and then created a new database as well
i got all my commands from this site http://blog.jasonmeridth.com/posts/postgresql-command-line-cheat-sheet/
last step was I imported a sql file by typing:
psql -d db_name_dev -U username_dev -f /www/dbexport.sql
Now when I go inside the database I created "db_name_dev" by typing
psql db_name_dev and check to see any content imported by typing \dt
I get
No relations found.
here is also a table and role list from my command line..
http://screencast.com/t/8ZMqBLNRb
I'm thinking my database might also have some access privilege issue..
also here is an additional issue i ran into.. hope this helps..
http://screencast.com/t/BJy0ZjrALm6h
thanks,
any feedback would be appreciated
ok so after a few research and readings, i found out my .sql file was empty.. here are some links ive read and learnt more about pg_dump command dbforums.com/showthread.php?1646161-Postgresql-Restores and pg_dump vs pg_dumpall? which one to use to database backups?

Dropping a postgres database in cmdline, still seeing the database when \list

I'm trying to drop my database and create a new one through the command line.
I login using psql postgres and then do a \list, see a list of the two databases i created which i now want to delete. so i tried using a DROP DATABASE databasename;
I don't see any error while executing that statement but when i try to \list again to see if that DB are deleted, i still see that that the DB exists. Can someone please tell me why this could happen? and how to surely delete those DB.
There are a couple caveats to DROP DATABASE:
It can only be executed by the database owner.
It cannot be executed while you or anyone else are connected to the target database.
I generally use the dropdb command-line tool to do this, since it's a wrapper around DROP DATABASE which doesn't require you to explicitly connect first. It still has the caveat that there can't be any users currently connected to the database, but it's generally quicker/easier to use.
I would recommend you try issuing a command like this:
dropdb -h <host> -U <user> -p <port> <name of db to drop>
Similarly, you can use the createdb command-line tool to create a database.
More info on DROP DATABASE: http://www.postgresql.org/docs/current/static/sql-dropdatabase.html
Edit:
Also, it is worth looking in the Postgres log (likely in /var/log/postgresql by default) to see if perhaps there is anything in there that wasn't surfaced in the results.

Resources