Setting up postgres on VPS for production - postgresql

So, I have a Pyramid app with a postgres database on my local machine. I did a pg_dump to get a dump of the data in my database, called pg_dump_2014-04-22. I then git pushed this file, and did a git pull in the VPS to get the file.
Now, I have already installed postgres on my VPS. When I sudo -u postgres psql on my VPS, I can connect to it but there are no relations (naturally).
Both my username and database name are postgres.
So, I tried psql postgres < pg_dump_2014-04-22, but this gives the error psql: FATAL: role "root" does not exist.
I also tried pg_restore -h localhost -U postgres -d postgres pg_dump_2014-04-22, and that prompts me for my password, but then throws the error pg_restore: [archiver(db)] connection to database "postgres" falied: FATAL: password authentication failed for user postgres"
What am I missing here?

You first have to create a user and the database where you want to import your dump
su postgres
createuser root
createdb yourdb
Then import the dump with
psql -d yourdb -f pg_dump_2014-04-22

Related

pg_dump: How to set username and password for the *.sql output file?

I want to create a local copy of an external postgres database. I am asked for the password of the local copy. Where can I set/access this password? I never set a password.
pg_dump -h external_hostname -p 5432 -U postgres db > db_copy.sql
createdb -p 5432 -h 127.0.0.1 -U postgres db_copy
I get the error
createdb: could not connect to database template1: FATAL: password authentication failed for user “postgres”
First you should find your pg_hba.conf file with
show hba_file;
Its place can be change but if you use debian based system, must be about etc/postgresql/9.6/main/pg_hba.conf
open this file and change
local all postgres peer
to
local all postgres trust
After all you should reload postgresql service from root, or easy way to reload is that, you run this select query by open psql :
select pg_reload_conf();

executing a .sql file through ubuntu command line for postgres

I have installed postgresql on ubuntu using:
$ sudo apt install postgresql
Now, I have a series of sql queries I would like to fire to create schemas and users and tables etc. I have put those queries in a .sql file as below:
$ sudo nano postgressetup.sql
CREATE SCHEMA schma;
CREATE USER a2i WITH PASSWORD 'password';
GRANT CONNECT ON DATABASE postgres TO schma;
This file has all the queries. I tried something like:
$ psql -U postgres -d postgres -a -f postgressetup.sql
and received error:
psql: FATAL: Peer authentication failed for user "postgres"
I want to know the way I can execute this .sql file.
Note: I've just installed postgres and no further operation is done on it. Any help is appreciated.
You can use the following command explicitly providing db context user
sudo -u postgres psql -U postgres -d postgres -a -f postgressetup.sql

Cannot access postgres PSQL

I have just installed postgres (mac os), however, when I go to access psql it tries to find the database not the user.
I know the username is benbagley (because that's my system name)
I have tried.
➜ ~ psql
psql: FATAL: database "benbagley" does not exist
and
➜ ~ psql -U benbagley
psql: FATAL: database "benbagley" does not exist
As stated in the manual psql assumes that database name is same as the username when the database is not provided explicitly and tries to connect with that database, which does not exist in your case.
Try connecting with default database which is postgres.
psql -U username -d postgres

Postgres - How to use psql to create a database with the -c command and password authentication?

If I run the following:
psql -h localhost -U admin -c "CREATE DATABASE sales OWNER admin;"
It returns:
psql FATAL: database "admin" does not exist
I need to use password authentication and have setup .pgpass as follows:
localhost:*:*:admin:admin
Any ideas?
By default, when you connect as a user, the database connected to is the DB of the same name as the user.
If you want to connect to a different DB, you must specify it in the psql command line. Since in this case you're trying to create the DB, you can't connect to it yet, you must connect to another DB (like template1 or postgres) that already exists.
E.g.
psql -h localhost -U admin template1 -c "CREATE DATABASE sales OWNER admin;"
^^^^^^^^^

Postgresql initial configuration: How to access as the postgres user?

After installing postgresql, I tried it out, typing createdb mydb, like it's written in the documentation. Then the following error occured:
createdb: could not connect to database postgres: FATAL: role "xxx" does not exist
I studied the documentation, where is said:
You will need to become the operating system user under which PostgreSQL was installed (usually postgres) to create the first user account
I tried this by accessing psql (in my case with sudo -u postgres psql, using Ubuntu 12.10).
But then what should I do?
if the db is owned by user postgres you can do the following
createdb -U postgres dbname
since by default postgresql will trust connections from localhost.
su - postgres
and after you have been logged in:
createdb mydb