After rebooting server on C9 my PostgreSQL database is empty - ruby

I had a Rails project on C9 with a SQLite 3 database. Recently I wanted to switch to a PostgreSQL database to be able to run a project on Heroku. All records from the SQLite 3 database were copied to the PostgreSQL database with the Sequel gem, without any issues. All records were passed to the new database and I was able to see it, however, after rebooting my C9 project, and starting the PostgreSQL service, and connecting it to my database, I found that there weren't any records inside the database. Or, it's not connected yet. I don't get it.
Every time I reboot the project, records from the PostgreSQL database disappear, I have to create a new database and migrate it again to see my records. What am I doing wrong?
Here is list of my actions:
sudo service postgresql start
psql -c "create database myapp_development owner=ubuntu"
sequel -C sqlite://db/development.sqlite3 postgres://ubuntu#""/myapp_development
Then update database.yml file records with:
Development:
- adapter: postgresql
- encoding: SQL_ASCII
- database: myapp_development
- pool: 5
- username: ubuntu
- password: password
Then run:
rake db:migrate
After these actions I can run the project and see my records.
After rebooting the project, I've tried to run the following commands:
sudo service postgresql start
psql myapp_development ubuntu
\c
but it does not help me to see any records.
Please advise me if I've missed any command.

Run psql as the postgres user:
sudo sudo -u postgres psql -d myapp_development -U ubuntu -W

Related

Postgres does not create role

I have installed postgres via brew. However, after running rake db:create I get error
role 'posgres' does not exist
I can not run psql as role of my user does not exist, which is logical, but I can not login with -u postgres either.
Any suggestions, how to create postgres user ?
It seems like the service is not running / turned on after instal.
Documentation reference:
https://www.postgresql.org/docs/current/static/server-start.html
Then perform initdb /[data location]
Then you will need to sudo to postgres user (the service user) to issue commands to the database.

postgres throws the error psql: FATAL: role "username" does not exist

Please, I need help with my postgres server. I have problems starting the server. I was using the app here a few months ago and everything was fine. Today I spent hours trying to start it yet couldn't. I then realised I had another instance of PostgreSQL installed. I uninstalled version 9.6 blocking my app.
Now after trying again, the PostgreSQL app throws the error when I use psql:
FATAL: role "username" does not exist
I didn't set the role or user but it somehow took my username and is throwing does not exist. Please, how do I start my server. I have seen someone with a similar issue here but unlike in his case, I didn't even get to start the server. not to speak of deleting the database. Please, how do I address this? Any help would be appreciated.
I also noticed he was able to execute
which psql
which returned his supposed psql path. It does not return anything in my case. Not sure how the configuration works, this is my second time using this database and it failed right away.
Thank you for your time, as answered in one of the SO question here. Running
sudo -u postgres -i
did give me access to postgres, but if you are using the postgres app like myself here . Running psql commands would not work for you as postgres is not installed globally on your machine. But running
'/Applications/Postgres.app/Contents/Versions/9.5/bin'/psql -p5432
which is the exact command the postgres app execute when you attempt to start the service works great and start the service for me. You can then proceed and add the role and database your postgres app looks for by default. So in summary. Below is what worked for me.
$sudo -u postgres -i
$'/Applications/Postgres.app/Contents/Versions/9.*/bin'/psql -p5432
#CREATE USER postgres SUPERUSER;
#CREATE DATABASE postgres WITH OWNER postgres;

Import a heroku postgres database dump into a docker database container with boot2docker

I'm running boot2docker on OSX 10.10. I have a database container set up so my databases don't get reset every time I start/stop a container. I would like to import a dump of a postgres database from heroku into my docker database. Is this possible to do?
I ended up finding this out with the help of a coworker. It's a little bit harder than just a regular old postgres database, but not much. This is based off this stackoverflow answer.
Generate your heroku database dump download url: heroku pgbackups:url
Start a bash shell on your postgres container. On my system this container was named pg: fig run db bash
Install curl: apt-get update && apt-get install curl
Download the database dump using curl: curl -o latest.dump [PASTE THE OUTPUT OF STEP 1 HERE]
Import the dump (note, database name and username can be found in fig.yml and database.yml respectively): pg_restore --verbose --clean --no-acl --no-owner -h [YOUR BOOT2DOCKER IP] -U [YOUR_USERNAME] -d [DATABASE_NAME] latest.dump
And there you have it!
If the last step fails with some kind of invalid database error, double check latest.dump with head latest.dump. If you feel like your database is not downloading correctly, you may want to manually download it via the web gui and upload it to another host, like drop box. Then you would substitute step 1 with whatever url your dump can be found at.

copying data from refinerycms in heroku to apache hosted website

I have a website already hosted on heroku.I added all content in the remote refinery.now i hosted the same website on apache using passenger.And i want to copy the content from the heroku refinery to the apache one.How do i go about it?
my database.yml file is as follows:
development:
adapter: mysql2
encoding: utf8
database: app_development
pool: 5
username: root
password: root
test:
adapter: mysql2
encoding: utf8
database: app_test
pool: 5
username: root
password: root
production:
adapter: mysql2
encoding: utf8
database: app_production
pool: 5
username: root
password: root
Im using ubuntu 13.10
Is this your new database.yml file? or were you using mysql on heroku too (most people use postgres)?
working on the assumption that you were on postgres on heroku and want to move to a mysql local database you will first need to copy your database information onto your new server.
For information on how to get the data off've your heroku database see here and here, the relevant line is here:
$ PGPASSWORD=mypassword pg_dump -Fc --no-acl --no-owner -h localhost -U myuser mydb > mydb.dump
Now you'll have a postgres backup file that will be all the sql commands needed to recreate the backup with all your data, you would think that since sql is a standard and mysql uses that too you could just do
mysql my_new_database_name < mydb.dump
Unluckily you're often not that lucky (I would recommend trying it, you never know with simple enough database structures) and you will then need to use a converter, this question recommends using pg2mysql.
This does of course bring up the question of why you want to move to a mysql installation?
Here is an article by digital ocean comparing sqlite, mysql and postgres, which has this to say about postgres.
PostgreSQL is the advanced, open-source [object]-relational database
management system which has the main goal of being standards-compliant
and extensible. PostgreSQL, or Postgres, tries to adopt the ANSI/ISO
SQL standards together with the revisions.
For my own opinion I quite like postgresql and find it has less unpleasant surprises than mysql but YMMV. Given the extra work in switching databases I'd probably be inclined to stick to Postgres but that's my opinion.

psql: FATAL: role "postgres" does not exist

I'm a postgres novice.
I installed the postgres.app for mac. I was playing around with the psql commands and I accidentally dropped the postgres database. I don't know what was in it.
I'm currently working on a tutorial: http://www.rosslaird.com/blog/building-a-project-with-mezzanine/
And I'm stuck at sudo -u postgres psql postgres
ERROR MESSAGE: psql: FATAL: role "postgres" does not exist
$ which psql
/Applications/Postgres.app/Contents/MacOS/bin/psql
This is what prints out of psql -l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
------------+------------+----------+---------+-------+---------------------------
user | user | UTF8 | en_US | en_US |
template0 | user | UTF8 | en_US | en_US | =c/user +
| | | | | user =CTc/user
template1 | user | UTF8 | en_US | en_US | =c/user +
| | | | | user =CTc/user
(3 rows)
So what are the steps I should take? Delete an everything related to psql and reinstall everything?
Thanks for the help guys!
NOTE: If you installed postgres using homebrew, see the comment from #user3402754 below.
Note that the error message does NOT talk about a missing database, it talks about a missing role. Later in the login process it might also stumble over the missing database.
But the first step is to check the missing role: What is the output within psql of the command \du ? On my Ubuntu system the relevant line looks like this:
List of roles
Role name | Attributes | Member of
-----------+-----------------------------------+-----------
postgres | Superuser, Create role, Create DB | {}
If there is not at least one role with superuser, then you have a problem :-)
If there is one, you can use that to login. And looking at the output of your \l command: The permissions for user on the template0 and template1 databases are the same as on my Ubuntu system for the superuser postgres. So I think your setup simple uses user as the superuser. So you could try this command to login:
sudo -u user psql user
If user is really the DB superuser you can create another DB superuser and a private, empty database for him:
CREATE USER postgres SUPERUSER;
CREATE DATABASE postgres WITH OWNER postgres;
But since your postgres.app setup does not seem to do this, you also should not. Simple adapt the tutorial.
For MAC:
Install Homebrew
brew install postgres
initdb /usr/local/var/postgres
/usr/local/Cellar/postgresql/<version>/bin/createuser -s postgres or /usr/local/opt/postgres/bin/createuser -s postgres which will just use the latest version.
start postgres server manually: pg_ctl -D /usr/local/var/postgres start
To start server at startup
mkdir -p ~/Library/LaunchAgents
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
Now, it is set up, login using psql -U postgres -h localhost or use PgAdmin for GUI.
By default user postgres will not have any login password.
Check this site for more articles like this: https://medium.com/#Nithanaroy/installing-postgres-on-mac-18f017c5d3f7
The key is "I installed the postgres.app for mac." This application sets up the local PostgreSQL installation with a database superuser whose role name is the same as your login (short) name.
When Postgres.app first starts up, it creates the $USER database,
which is the default database for psql when none is specified. The
default user is $USER, with no password.
Some scripts (e.g., a database backup created with pgdump on a Linux systsem) and tutorials will assume the superuser has the traditional role name of postgres.
You can make your local install look a bit more traditional and avoid these problems by doing a one time:
/Applications/Postgres.app/Contents/Versions/9.*/bin/createuser -s postgres
which will make those FATAL: role "postgres" does not exist go away.
createuser postgres --interactive
or make a superuser postgresl just with
createuser postgres -s
And if you are here in 2023 and wondering what works with the latest Postgres on the latest macOS (macOS Monterey )
follow this:
brew install postgresql
createuser -s postgres
brew services restart postgresql
This happens when you run initdb with a user whose ID is not postgres, without specifying the postgres username with --username=postgres or -U postgres.
The database cluster is then created with the system's user account that you used to run initdb, and it is given superuser permissions.
To fix it, simply create a new user named postgres with the option --superuser using the createuser utility that comes with Postgres. The utility can be found in the Postgres' bin directory. e.g.
createuser --superuser postgres
If you have a custom hostname or port then be sure to set the appropriate options.
Don't forget to delete the other user account that was created for you by initdb.
If you installed postgres from brew, run this in your terminal :
/usr/local/opt/postgres/bin/createuser -s postgres
First you need create a user:
sudo -u postgres createuser --superuser $USER
After you create a database:
sudo -u postgres createdb $USER
Change $USER to your system username.
You can see the the complete solution here.
I needed to unset $PGUSER:
$ unset PGUSER
$ createuser -s postgres
For me, this code worked:
/Applications/Postgres.app/Contents/Versions/9.4/bin/createuser -s postgres
it came from here:
http://talk.growstuff.org/t/fatal-role-postgres-does-not-exist/216/4
If you installed postgres from Brew and are using an Apple Silicon (M1) mac, run this in your terminal:
/opt/homebrew/opt/postgresql/bin/createuser -s postgres
If you're using an Intel (x86) mac, run this in your terminal:
/usr/local/opt/postgres/bin/createuser -s postgres
Running this on the command line should fix it
/Applications/Postgres.app/Contents/Versions/9.4/bin/createdb <Mac OSX Username Here>
This article helped me to solve same issue psql: FATAL: role “postgres” does not exist.
I am using mac, so I entered this command in terminal:
createuser -s postgres
And it worked for me.
This worked for me
createuser -s postgres
note: I'm using mac catalina
If you're using docker, make sure you're NOT using POSTGRES_USER=something_else, as this variable is used by the standard image to know the name of the PostgreSQL admin user (default as postgres).
In my case, I was using this variable with the intent to set another user to my specific database, but it ended up of course changing the main PostgreSQL user.
We have a db named postgres after brew install postgresql and brew services start postgresql. So we can open psql like this by default.
psql postgres
And then we can add users with any name like this in that psql console.
CREATE USER postgres
And if we want a super user, then we can add SUPERUSER at the end.
For m1 chips, if you have not installed postgresql package by homebrew, install it in terminal with:
brew install postgre
then create a username manually by:
/opt/homebrew/bin/createuser -s <username>
your error is probably fixed; but if you occur the error
FATAL: database "databasename" does not exist
then you have to create your database manually by:
/opt/homebrew/bin/createdb -U <username> <databasename>
Dropping the postgres database doesn't really matter. This database is initially empty and its purpose is simply for the postgres user to have a kind of "home" to connect to, should it need one.
Still you may recreate it with the SQL command CREATE DATABASE postgres;
Note that the tutorial mentioned in the question is not written with postgres.app in mind.
Contrary to PostgreSQL for Unix in general, postgres.app tries to look like a normal application as opposed to a service that would be run by a dedicated postgres user having different privileges than your normal user. postgres.app is run and managed by your own account.
So instead of this command: sudo -u postgres psql -U postgres, it would be more in the spirit of postgres.app to just issue: psql, which automatically connects to a database matching your users's name, and with a db account of the same name that happens to be superuser, so it can do anything permissions-wise.
This is the only one that fixed it for me :
createuser -s -U $USER
For what it is worth, i have ubuntu and many packages installed and it went in conflict with it.
For me the right answer was:
sudo -i -u postgres-xc
psql
I've faced similar problem today, actually i was not sure what was the username. Here is the 2 thing, if you are under enterprise and don't have system admin access the postgres will create your enterprise username as the postgres admin username. If you install through Homebrew it will definitely happening. In that case simply run your psql service with brew and do an echo of the username
brew services start postgresql
then
echo $USER
You will see your username of the postgres user.
If you are experiencing this problem right after running a docker container try destroying the container and recreating it. That solved it for me:
docker-compose down
docker-compose up --force-recreate
This should recreate the db with postgresuser as default user
With a new mac (M1) and latest postgres (14.0) installed via homebrew, nothing helped me from this topic, but i just reinstalled postgres and it helped:
brew services stop postgresql
rm -rf /opt/homebrew/var/postgres/*
brew reinstall postgresql
initdb --locale=C -E UTF-8 /opt/homebrew/var/postgres
brew services restart postgresql
So, it's a miracle or something like that...
Then just:
psql -d postgres
If you are a MAC (M1) user and installed the Postgres using HomeBrew then follow these steps:
Check your Postgres location using which psql
then run the command /opt/homebrew/bin/createuser -s postgres if the output for the first command is /opt/homebrew/bin/psql
The idea is to create a user named 'postgres' using the Postgres installation location. So you may need to change the command based on the location of your Postgres.
On Ubuntu system, I purged the PostgreSQL and re-installed it. All the databases are restored.
This solved the problem for me.
Advice - Take the backup of the databases to be on the safer side.
Context
I am adding an answer for a case I have not seen here, which is an edge case if you have multiple users on the same machine and the user who is trying to use postgres services is not the user who installed postgres on the machine.
What I have tried
Among other similar commands, for me all these commands failed:
createuser -s [your username]
# createuser: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL: role "[your username]" does not exist
createuser -s postgres
# createuser: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL: role "[your username]" does not exist
sudo -u postgres createuser --superuser [your username]
# sudo: unknown user: postgres
# sudo: error initializing audit plugin sudoers_audit
psql -U postgres
# psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL: role "postgres" does not exist
Reason
The reason is because neither postgres role nor [your username] (aka whoami on your command line) are in postgres.
Solution
In such edge case I had to first login with the user who installed postgres:
sudo su - [username that installed postgres]
And then create a role for my new user:
createuser -s [your username]
On Mac, executing
createuser -s postgres
in a terminal worked.
I don't think that sudo is needed here because psql -l returns a list of databases. This tells me that initdb was run under the user's current user, not under the postgres user.
You can just:
psql
And continue the tutorial.
I would suggest A.H's general points of creating the postgres user and db because many applications may expect this to exist.
A brief explanation:
PostgreSQL will not run with administrative access to the operating system. Instead it runs with an ordinary user, and in order to support peer authentication (asking the OS who is trying to connect) it creates a user and db with the user that runs the initialization process. In this case it was your normal user.
I became stuck on this issue having executed brew services stop postgresql the day prior. The day following: brew services start postgresql would not work. This is because as is shown when you install using homebrew. postgresql uses a launchd ... which loads when your computer is powered on. resolution:brew services start postgresql Restart your computer.
The \du command return:
Role name = postgres#implicit_files
And that command postgres=# \password postgres return error:
ERROR: role "postgres" does not exist.
But that postgres=# \password postgres#implicit_files run fine.
Also after sudo -u postgres createuser -s postgres the first variant also work.

Resources